VBA in Excel to format all tables in Word document

bisel

Board Regular
Joined
Jan 4, 2010
Messages
101
Hello All,

My inexperience is showing here and I hope someone can help.

I have some VBA code in an Excel workbook that creates a Word document then copies various objects from the workbook to various associated bookmarks in the Word document.

After I have completed copying and pasting some ranges to Word as Word tables, I want to go and for each table set the AutoFitBehavior to fit all the tables to the window. I am trying use this bit of code ...

Code:
....

On Error GoTo 0
For Each mytable In newWord.tables
mytable.AutoFitBehavior wdAutoFitWindow    
Next

....
I do not get any errors, but it does not work. I tried enclosing the wdAutoFitWindow inside parantheses like this ...
Code:
....

On Error GoTo 0    
For Each mytable In newWord.tables        
mytable.AutoFitBehavior (wdAutoFitWindow)    
Next

....
but no difference.

Appreciate any insight on how I might fix this.

Regards,

Steve
 
Last edited:

Some videos you may like

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,459
Office Version
365
Platform
Windows
Steve

What happens if you remove On Error...?

Do you get any errors then?

P.S. Are you using early or late binding?
 

bisel

Board Regular
Joined
Jan 4, 2010
Messages
101
Hello Norie,

Removing "On Error" has no effect. And, I am using late binding.

Thanks,

Steve
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,459
Office Version
365
Platform
Windows
Steve

If you are using late-binding you need to either replace the constant wdAutoFitWindow with its actual value or declare an appropriate constant.
Code:
Const wdAutoFitWindow = 2
 

bisel

Board Regular
Joined
Jan 4, 2010
Messages
101
Thank you, Norie. Your suggestion is the solution I was looking for.

My VBA is like this ...

Code:
On Error GoTo 0
    For Each mytable In newWord.tables 
         mytable.AutoFitBehavior 2 
    Next
This does exactly what I was hoping which was to autofit each table to the window.

Best regards,

Steve
 

Forum statistics

Threads
1,089,273
Messages
5,407,319
Members
403,133
Latest member
Whatsit6743

This Week's Hot Topics

Top