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.
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.