VBA in Excel to format all tables in Word document

bisel

Board Regular
Joined
Jan 4, 2010
Messages
223
Office Version
  1. 365
Platform
  1. Windows
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 ...

PHP:
....

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 ...
PHP:
....

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:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Steve

What happens if you remove On Error...?

Do you get any errors then?

P.S. Are you using early or late binding?
 
Upvote 0
Hello Norie,

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

Thanks,

Steve
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,382
Messages
6,119,194
Members
448,874
Latest member
Lancelots

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top