Excel VBA sheet to word autofit

excel_learnerz

Board Regular
Joined
Dec 12, 2018
Messages
73
Hello
With the below code I can export used ranged as table into Microsoft word.
When it goes to word the table isn't autofitted, so half of it is not appearing correctly.
Is there a way for it to be formatted correctly in the word document
Thanks in advance


Sub annex()
With Sheets("Sheet1").Select
Set obj = CreateObject("Word.Application")
obj.Visible = True
Set newObj = obj.Documents.Add
ActiveSheet.UsedRange.Copy
newObj.Range.Paste
Application.CutCopyMode = False
obj.Activate

newObj.SaveAs Filename:="C:\temp" & ActiveSheet.Name
End With
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hello
With the below code I can export used ranged as table into Microsoft word.
When it goes to word the table isn't autofitted, so half of it is not appearing correctly.
Is there a way for it to be formatted correctly in the word document
That all depends on what you mean by 'formatted correctly'. Try one of:
newObj.Range.PasteExcelTable False, False, False
newObj.Range.PasteAndFormat 16 'wdFormatOriginalFormatting
newObj.Range.PasteAndFormat 19 'wdUseDestinationStylesRecovery
 
Upvote 0

Forum statistics

Threads
1,215,143
Messages
6,123,275
Members
449,093
Latest member
Vincent Khandagale

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