![]() |
Unable to add tab stops to Word document via Excel VBA
1 Attachment(s)
Hi All,
I have several utility applications that I run on Word documents using Excel VBA. I use Excel to track the documents. I wanted to write some code that would programmatically redo the footer in a Word document, with the company name at the left margin, the page number centered, and "confidential information" on the right margin. I originally wrote this in Word VBA, and everything worked fine with some tweaking from a friend. However, when I adjusted the code to run from Excel, I am unable to create tabs in the footer. When finished, the footer should look like the attached image. Most of the code works as expected. I just can seem to set the tabs with Excel VBA. The code is below. For sake of illustration, I am only working on the primary footer. Thanks in advance for any assistance. Roy Code:
Sub createFooter() |
I'm thinking you need to convert the word constants into their integer values
wdAlignTabCenter = 1 wdAlignTabRight = 2 wdTabLeaderSpaces = 0 Code:
'So try changing this line |
Thanks, Andrew, for the reply! I had a weird response. I ran the code as you suggested, and it worked. I closed my test file without saving and re-ran the code. This time, the code behaved as before and didn't create the tab stops. I didn't change anything. Not sure why it works sometimes and not others.
Thanks again, Roy |
If there is a reference to the Word Object Library then the wdConstants appear to work for me so my earlier advice should be ignored. I had a fiddle with your code to actually test it and adapt it for various page setups. It works for me on multiple files so I'm not sure why you would get variable results. Can you test this code on your machine?
Code:
Sub createFooter() |
Many, thanks, Andrew! Truly appreciate your efforts! This worked. Out of curiosity, do you think it’s not possible to set a positional tab stop in Word from Excel VBA?
|
I can't see any reason why the positioned tab would not work with your code but it would be problematic because you don't know the page width of every section.
If the page setup varies across sections or documents then using a static 6.5 inches as your assumed width is not going to work with every section. The 'special' alignment tabs adapt to the page setup width of every section so you could even use 'same as previous' for all footers after the first section. |
Thank you again, Andrew! Very kind of you to help me!
Best wishes, Roy |
Please mark thread as solved. Thx
|
All times are GMT -7. The time now is 07:56 PM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by
DragonByte SEO (Lite) -
vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft