Writiing to Word document bookmark from Excel VBA

Dennis Hancy
Dennis Hancy used Ask the Experts™
I have an Excel VBA script that writes to a Word document which is created from an existing template.

In the footer of this template, there are three sections.  From left to right:

- Date/Time (left justified)
- Descriptive Text
- Page Number (right justified)

The Descriptive Text is identified by a bookmark I created in the template.  It should be centered.  

Up until now, the Descriptive Text has, in fact, been centered (or so it seemed).  However, when I added another word to the end of this text, it becomes off-center.  It looks like it is left justified starting at the insertion point of the defined bookmark.  

I tried changing the alignment of the text to "center", but then it tries to center the date/time on the left as well.

It would be so nice if I could demo this via a screen share or something!

Any guidance, suggestions, or encouraging "words" on how to accomplish this are most appreciated.  Thank you !!!
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2012

You don't say how you are implementing the centring, but it might help to know that the default paragraph format for the footer is set up to help with that layout.  It has a Centre tab stop in the centre and a Right tab stop on the right, so you need:

Date/Time<tab>Descriptive Text<tab>Page Number
where <tab> is a tab character


Hi.  Sorry for the delay in responding.

I checked my Word template file.  I do have a center tab set up in the footer, and the bookmark is located on that tab.

But when I write out my descriptive text to that bookmark, it still shows up left justified instead of centered.
Top Expert 2012
Be sure to distinguish between a tab and a tab stop. Tab stops are part of a paragraph format. Tab characters are part of the text.
The footer in the attached document here has a bookmark between two tab characters.
To ensure that the bookmark contains (as opposed to marking the start or end of) the incoming text, and also to make sure that the bookmark is not overwritten, I use code like this:
Sub InsertTextIntoBookmark()
    Dim bmk As Bookmark
    Dim strBookmarkName As String
    Dim doc As Document
    Dim rng As Range
    strBookmarkName = "MyBookmark"
    Set doc = ActiveDocument
    Set rng = doc.Bookmarks(strBookmarkName).Range
    rng.Text = "Centre text"
    doc.Bookmarks.Add strBookmarkName, rng
End Sub

Open in new window

Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.


Thank you!!

In my case, I don't have code that manipulates the template itself; however, your statement "ensure that the bookmark contains (as opposed to marking the start or end of) the incoming text" was the key.

I compared your template with my own and noticed one subtle difference.  

If my cursor is on the bookmark, and I press either the left or right arrow, the cursor navigates to either side immediately (this is on your document).

When I did the same on mine, I had to press the arrow key twice before it moved. It's almost like there was a space embedded around the bookmark.

Anyway, I deleted the bookmark, and recreated it.  Now when I press the arrow key, it behaves the same was as yours.

I ran my script and the text is now centered.

Thank you again.  I really appreciate your help.



Thank you!!  

As noted in my last post, this is now working as expected.
Top Expert 2012

As WS put it: "All's well that ends well"

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial