0
votes

I am copying a cell range from Excel to Word which contains several tables. I need to have my document set to portrait. The issue is the copied tables width is larger than width of Word document page. So I can only see part of the table, the other part goes out of the Word document, hence can't see it.

Once the Word doc is created I can manually shrink the table width to fit Word documents page width. How can I achieve that in programmatically using Office Interop?

document.PageSetup.PageWidth = (float)500 didn't work out for me. I saw there is a property as AutoFitBehavior: Word.WdAutoFitBehavior.wdAutoFitWindow but not sure how it can be apply to the Word document.

Appreciate any help on this?

1

1 Answers

2
votes

As you asked some questions in your question body, I can answer some of them as:

I need to have my document set to portrait:

You should insert a section break then change the orientation:

doc.Words.Last.InsertBreak(Wd.WdBreakType.wdSectionBreakNextPage);
doc.Sections.Last.PageSetup.Orientation = orientation;

Or set orientation after creating document

doc.PageSetup.Orientation = WdOrientation.wdOrientLandscape;

Once the Word doc is created I can manually shrink the table width to fit Word documents page width. How can I achieve that in programmatically using Office Interop?

You need just to set PreferredWidth too 100 percent after setting its type to wdPreferredWidthPercent:

table.PreferredWidthType = Wd.WdPreferredWidthType.wdPreferredWidthPercent;
table.PreferredWidth = 100.0f;

I saw there is a property as AutoFitBehavior: Word.WdAutoFitBehavior.wdAutoFitWindow but not sure how it can be apply to the Word document:

You can use AutoFitBehavior like this:

table.AllowAutoFit = true;
table.AutoFitBehavior(Wd.WdAutoFitBehavior.wdAutoFitWindow);