I’m wondering if I can do the following:
- Find a bookmark within a document
- Identify the page number this bookmark is on
- Insert another document before the identified page
My code so far:
Sub InsertDoc
Dim MasterDoc As Document
Dim bookmarkRng As Range
Dim pageNo As Long
Set MasterDoc = Documents.Open("Master.docx")
Set bookmarkRng = MasterDoc.Bookmarks("TheBookmark").Range
pageNo = bookmarkRng.Information(wdActiveEndPageNumber)
'1.This is where I need to specify to insert before/after the found page, not sure how
MasterDoc.InsertFile FileName:="AnotherDoc.docx"
'2.Is it possible to use this syntax instead and specify before/after insertion of a whole file?
MasterDoc.Bookmarks("TheBookmark").Range.InsertFile FileName:="AnotherDoc.docx"
MasterDoc.Save
MasterDoc.Close
Set MasterDoc = Nothing
End Sub
Any help is much appreciated, thanks.
It’s best not to think in terms of pages, which ironically is an ill defined concept in Word. But if you’re inserting material, you have to insert it at specific location anyhow. Since you find a range for the bookmark, and it looks like you want to insert after the bookmark, use the following:
Also, you could remove bookmarkRng and pageNo, then, unless you need them later for some reason.