The Anchorage
Personal website of Gregory K. Maxey, Commander USN (Retired)
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!
This Microsoft Word Help & Microsoft Word Tips page will show you how to use VBA to insert text "at" or "in" a bookmark and illustrate the advantages of inserting "in" a bookmark.
Bookmarks, as you may know, identifies a location in a document. What you may not know, is that a bookmark defines a document range.
For this tips page I am going to create two empty bookmarks named bmAtBookmark and bmInBookmark.
Inserting text at a bookmark with VBA is very simple. A single line of code is all you need (See InsertAtBookmarkI and InsertAtBookmarkII in the code pane below:
Sub InsertAtBookmarkI() ActiveDocument.Bookmarks("bmAtBookmark").Range.InsertAfter "Some text here" lbl_Exit: Exit Sub End Sub Sub InsertAtBookmarkII() ActiveDocument.Bookmarks("bmAtBookmark").Range.InsertAfter " Some more text here" lbl_Exit: Exit Sub End Sub
When you execute InsertAtBookmarkI, everything appears fine.
The complication arises when you want to insert additional or modified text at the bookmark. Execute InsertAtBookmarkII above and review the result:
Inserting text "in" a bookmark takes just a few extra lines of code but the results are well worth the effort. See: InsertInBookmarkI and InsertInBookmarkII in the code pane below:
Sub InsertInBookmarkI() Dim oRng As Word.Range Set oRng = ActiveDocument.Bookmarks("bmInBookmark").Range oRng.Text = ActiveDocument.Bookmarks("bmInBookmark").Range.Text & "Some text here." ActiveDocument.Bookmarks.Add "bmInBookmark", oRng lbl_Exit: Exit Sub End Sub Sub InsertInBookmarkII() Dim oRng As Word.Range Set oRng = ActiveDocument.Bookmarks("bmInBookmark").Range oRng.Text = ActiveDocument.Bookmarks("bmInBookmark").Range.Text & "Some more text here." ActiveDocument.Bookmarks.Add "bmInBookmark", oRng lbl_Exit: Exit Sub End Sub
When you execute InsertInBookmarkI, the inserted content is bound by the bookmark range.
If you want to add additional text, execute InsertInBookmarkII.
Note: Modifying or defining a bookmark range with a VBA procedure destroys the bookmark! This is why putting text "in" a bookmark requires these extra steps:
You should now see that by putting text "in" vice "at" a bookmark, you allow the document user to do something and then change his or her mind and do something different without messing up the document.
Delete the text following the bookmark bmAtBookmark and run the procedure InsertAtBookmarkIII provided below.
Sub InsertAtBookmarkIII() ActiveDocument.Bookmarks("bmAtBookmark").Range.InsertAfter InputBox("What is your favorite color?") lbl_Exit: Exit Sub End Sub
As before, the result in the document looks fine. Now you've change your mind so run the code again the result is not so good.
Now try the following code and see if you aren't pleased with the result:
Sub InsertInBookmarkIII() Dim oRng As Word.Range Set oRng = ActiveDocument.Bookmarks("bmInBookmark").Range oRng.Text = InputBox("What is your favorite color?") ActiveDocument.Bookmarks.Add "bmInBookmark", oRng lbl_Exit: Exit Sub End Sub
If you will write to bookmark ranges frequently or if you have a project involving several bookmark ranges, you should consider using a call in your procedure to a separate procedure just for writing to the bookmark range. This can eliminate using repetitive code in your procedures.
Sub DemoWriteToBookmarkRange() Dim strInput As String strInput = InputBox("What is your favorite color?") WriteToBookmarkRange ActiveDocument, "bmInBookmark", strInput strInput = InputBox("What is your favorite food?") WriteToBookmarkRange ActiveDocument, "bmInBookmarkFood", strInput strInput = InputBox("What is your favorite soft drink?") WriteToBookmarkRange ActiveDocument, "bmInBookmarkDrink", strInput lbl_Exit: Exit Sub End Sub Sub WriteToBookmarkRange(ByRef oDoc As Document, bmName As String, strContent As String) Dim oRng As Word.Range If oDoc.Bookmarks.Exists(bmName) Then Set oRng = oDoc.Bookmarks(bmName).Range oRng.Text = strContent oDoc.Bookmarks.Add bmName, oRng Else MsgBox "An error occurred while processing your document." _ & vbCr & "The bookmark " & Chr(34) + bmName + Chr(34) & " does not exist.", _ vbInformation, "Missing Bookmark" End If lbl_Exit: Exit Sub End Sub
Inserting a file content using the .InsertfFile method in a bookmark is a little tricky. This is because when you use a range object with the .InsertFile method (e.g., oRng.InsertFile "D:\FileToInsert.docx"), the range ends up collapsed at the start of the inserted text vice at the end as would be expected.
You can use the following method to resolve this issue:
Sub InsertFileInBookmark() Dim oRng As Range, oRngNC As Range Set oRng = ActiveDocument.Bookmarks("bmInBookmark").Range Set oRngNC = oRng.Characters.Last oRng.InsertFile ("D:\FileToInsert.docm") oRngNC.Characters.Last.Previous.Delete oRngNC.End = oRngNC.End - 1 ActiveDocument.Bookmarks.Add "bmTarget", oRngNC lbl_Exit: Exit Sub End Sub
See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.
That's it! I hope you have found this tips page useful and informative.
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!