Just not getting an understanding of how VBA is meant to be used to build a document

J

John Clark

Help?

I am just not getting an understanding of how to programmatically manage
the range object while building a document using VBA script - the code I
am using is below, but it just doesn't feel right to me... I am
constantly getting confused as to when to collapse, when the range has
to move, when to use InsertBefore and when to use InsertAfter...

In my mind, starting with an empty document, the flow should be

1. Set range to document contents
2. collapse range towards the end
3. Set formatting characteristics
4. Insert text (which will adopt above formatting characteristics)
5. Insert paragraph mark
6. Collapse range towards end
7. GOTO 3

However, this doesn't seem to work.

What I have developed for a simple title page thorugh a shocking amount
of trial and error is the following:

Set MSWord = CreateObject("Word.Application")
MSWord.Visible = True
Set myDoc = MSWord.Documents.Add

Set myRange = myDoc.Content
myRange.Font.Name = "Arial"
myRange.Font.Bold = True
myRange.Font.Kerning = 14
myRange.Font.Size = 18
myRange.ParagraphFormat.SpaceBefore = 200
myRange.ParagraphFormat.SpaceAfter = 3
myRange.ParagraphFormat.OutlineLevel = 1
myRange.ParagraphFormat.Alignment = wdAlignParagraphCenter
myRange.InsertBefore "Company Name"
myRange.Collapse wdCollapseEnd
myRange.InsertBreak wdLineBreak
myRange.Collapse wdCollapseEnd
myRange.InsertBefore "Division Name"
myRange.Collapse wdCollapseEnd
myRange.InsertParagraphAfter
myRange.Collapse wdCollapseEnd
myRange.ParagraphFormat.SpaceBefore = 30
myRange.InsertAfter "Subject Area"
myRange.Font.Size = 28
myRange.Collapse wdCollapseEnd
myRange.InsertBreak wdLineBreak
myRange.Collapse wdCollapseEnd
myRange.InsertBefore "Report Name"
myRange.Collapse wdCollapseEnd
myRange.InsertParagraphAfter
myRange.Collapse wdCollapseEnd
myRange.Font.Name = "Arial"
myRange.Font.Bold = True
myRange.ParagraphFormat.SpaceBefore = 6
myRange.ParagraphFormat.Alignment = wdAlignParagraphCenter
myDoc.Fields.Add Range:=myRange, Type:=wdFieldDate,
Text:="\@ ""M/d/yyyy"" \* MERGEFORMAT"
Set myRange = myDoc.Content
myRange.Collapse wdCollapseEnd
myRange.InsertParagraphAfter

What I am hoping at this point is that most of you are now laughing at
my feeble attempt at this and will now respond with the "recommended"
way to do what I am attempting to do above. In my estimation, there are
just too many things that are awkward about the above code such as:

* In my opinion there are way too many Collapses in the code above - I
just don't think that you are supposed to have to collapse the range
after every single insertion...
* The line that set's myRange.Font.Size = 28 won't work if it preceeds
the myRange.InsertAfter "Subject Area", and yet it does work where it
is... I guess I understand why it works where it is, but I don't
understand why it doesn't work before the preceeding line, especially
since the myRange.Font.Size = 18 line works where it is, and the
myRange.ParagraphFormat.SpaceBefore = 30 line works where it is.
 
S

Shauna Kelly

Hi John

I sympathize with all that mucking around. There are two things that can
help:

1. Define styles that store the formatting you want, and apply the style to
the appropriate range. That cuts down some of the code. This is appropriate
if the text you want to insert changes each time you run the code.

or

2. If the text is static, you can avoid all the collapsing and inserting.
Instead, open the template you're working on. Type the content you want,
formatted as you want, into the template. Select it and save it as an
AutoText in the template. Then use something like the following to insert it
(you'll need to add some error checking to cope if the AutoText is not in
the template):

Sub InsertText()

Dim oTemplate As Word.Template
Dim oAT As Word.AutoTextEntry
Dim oRange As Word.Range

'Identify the range where you want to insert the material
Set oRange = ActiveDocument.Range
'And collapse it
oRange.Collapse wdCollapseEnd

'Get the AutoText to insert
Set oTemplate = ActiveDocument.AttachedTemplate
Set oAT = oTemplate.AutoTextEntries("MyAutoText")

'Insert it
oAT.Insert Where:=oRange, RichText:=True

End Sub


There's more about AutoTexts at
Using AutoText
http://www.word.mvps.org/FAQs/Customization/AutoText.htm


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top