Link to home
Start Free TrialLog in
Avatar of Dennis Hancy
Dennis Hancy

asked on

Writiing to Word document bookmark from Excel VBA

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 !!!
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of Dennis Hancy
Dennis Hancy

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.


Dennis
Thank you!!  

As noted in my last post, this is now working as expected.
As WS put it: "All's well that ends well"