I am trying to insert bookmarks in a Word document using a
Word object from within VBScript. This is what I want
accomplished -
1. Create a Word object from VBScript.
2. Insert a bookmark.
3. Insert text at the bookmark.
4. Insert a new bookmark at the end of the document.
5. Add new text to the newly created bookmark.
6. Iterate through Step 4 & 5 as many times as needed.
7. Save the Word document.
Would someone have any information or code to accomplish
this. Thanks for your help.
Here are some bits and pieces (and links) to get you going.
In general: keep in mind that automating Word from within VBScript is nothing else then automating Word from within another Office application.
BUT (there's always a but ;-) VBScript doesn't know (AFAIK) any variable types. This means that you actually need to declare any variable as a variant AND therefor you can' t use the Word constants for the various values. Instead you need to use the numeric values for them. To find those values, open the objectbrowser in the VBE of Word (press F2 to do that) and search for the value.
> 1. Create a Word object from VBScript.
-Describes above technique, which is also named Late Binding.
Early vs. Late Binding
http://www.mvps.org/word/FAQs/InterDev/EarlyvsLateBinding.htm
- Some example code that starts Word etc.
Just modify it so it uses late binding (see previous article)
"Control Word from Excel"
http://www.mvps.org/word/FAQs/InterDev/ControlWordFromXL.htm
> 2. Insert a bookmark.
oWdDoc.Bookmarks.Add "MyBookmark",selection.Range
where oWdDoc is the variable that holds your document object
> 3. Insert text at the bookmark.
"Inserting text at a bookmark without deleting the bookmark"
http://www.mvps.org/word/FAQs/MacrosVBA/InsertingTextAtBookmark.htm
> 4. Insert a new bookmark at the end of the document.
Dim oWdRange As Variant
Set oWdRange = oWdDoc.Content
oWdRange.Collapse direction:=0
oWdDoc.Bookmarks.Add Name:="BookmarkName", _
Range:=oWdRange
> 5. Add new text to the newly created bookmark.
see 3
> 6. Iterate through Step 4 & 5 as many times as needed.
Define a loop. Don't forget you can use the same bookmarkname only once in the document. If you don't know in advance how many you need, you need to create the bookmarkname from within code
> 7. Save the Word document.
oWdDoc..SaveAs filename:="Path and filename"
Hope this helps,
regards,
Astrid
So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/