Hi Word gurus,
I am trying to do followings but not sure if it is doable. Greatly
appreciated for any help.
My whole purpose is to enforce the formating of Word documents that are
based on certain template. Word 2003 has a very good new feature, locking
down Word styles. so I am going to create a template, define some styles, and
then lock down the formating by allowing only some styles using the
"Protecting document" tool.
When a new Word document is created based on the template with styles locked
down, new styles can not be created and existing styles can not be modified.
The problem is: you can still switch from one to another style. For example,
I want the style of the title of the document to be always "Heading 1", but
users can switch it to "Heading 2". This can be solved by some VBA + Word
API, for example:
ActiveDocument.Paragraphs(1).Style = "Heading 1"
I also want to enforce the order of formating styles. For example, I want
the first paragraph is in "Heading 1" and it is followed by a paragraph in
"Heading 2", which is in chapter title level, and the title of chapters, here
in "Heading 2", will be followed by some parahraphs that are in "Body Text"
style. etc, etc:
Heading 1
Heading 2
Body Text
...
Body Text
This still can be done by iterating the paragraphs using the Paragraphs
collection.
Now the real problem: how to handle Tables and Lists that are mixed with
Paragraphs? For example, I want:
Heading 1
Heading 2
Body Text
...
Body Text
Heading 3
A Table (say 3X4 grid table)
Heading 4
Body Text
A numbered list.
Heading 5
In Word, you have collections for paragraphs, Tables, Lists etc but these
collections are not related in some way. For example, how do I know a table
is after "Heading 3"? or a paragraph is after a table?
What I need is: A collection of objects for a document and each object has a
type property. The object collections should include everything of the Word
content. I then can iterate the document from top to bottom:
Foreach obj in ActiveDocument.Objects
select case obj.Type
case "Paragraph"
...
case "Table"
...
end select
end for.
The problem is: I do not see the object model exists in Word, am I right? or
it is there but I am blind?
How should I do this kind of thing if it is not available from Word.
Thanks
Ken
Helmut Weber - 10 Jan 2006 09:52 GMT
Hi Ken,
I don't think to do it the way you imagine is possible,
though maybe somebody can proof, that I am wrong.
For checking properties of objects which precede or
follow other objects, check help for:
first, next, last, previous:
Examples:
With ActiveDocument.Tables(1).Range
MsgBox .Paragraphs.First.Previous.Style
End With
With Selection.Paragraphs
MsgBox .First.Previous.Range.Tables.Count
End With
--
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
Kenneth - 10 Jan 2006 13:14 GMT
Thanks Helmut. I will try your suggestion. Ken
> Hi Ken,
>
[quoted text clipped - 20 lines]
> "red.sys" & chr(64) & "t-online.de"
> Word 2002, Windows 2000