We help IT Professionals succeed at work.

Writiing to Word document bookmark from Excel VBA

Dennis Hancy
Dennis Hancy asked
on
160 Views
Last Modified: 2017-03-16
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 !!!
Comment
Watch Question

GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
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

Author

Commented:
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.
Retired
CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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

Author

Commented:
Thank you!!  

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

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.