I have been struggling with the attached VBA code which tries to set the page orientation when exporting an Excel table to Word. It is easy enough to do for one table, but I am trying to export several tables some that should be portrait and others landscape. Switching from one format to the other within the new Word document is the issue. I have tried various ways to add section breaks, but I have failed to do anything close to what I want.
The snippet below is run in a loop that exports each table as the form's control's are checked for a selection.
The code depends on a user's selections on a form so they may differ each time. To be more easily read I have only included the code sections which actually deal with the new Word document (newDoc) formatting. What is shown is the last incarnation of the code, there have been many versions.
What I need is a way to run the loop (and the attached code) so that each table is oriented according to its predetermined needs, either portrait or landscape in the new Word document.
Select Case intDoc ' set the Word pages based on the selected document
Case 1, 4, 5, 6 ' Estimate(1), Invoice(4), Scope of Work-->Orientation = wdOrientPortrait
' paste the table into the newDoc and set some options
With newDoc
.ActiveWindow.View.ShowAll = False ' Hide all formatting marks
.ActiveWindow.View.ShowHiddenText = False ' Hide all hidden text
.Paragraphs(.Paragraphs.Count).Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=True
.Content.InsertParagraphAfter
.Range(.Content.End - 1).InsertBreak Type:=wdSectionBreakNextPage 'wdPageBreak
With .PageSetup
.Orientation = wdOrientPortrait
.RightMargin = Application.InchesToPoints(0.75)
.LeftMargin = Application.InchesToPoints(0.75)
End With
End With
' Autofit the table to the page
Set wordTbl = newDoc.Tables(newDoc.Tables.Count)
wordTbl.AutoFitBehavior (wdAutoFitWindow)
Case 2, 3 ' the Detail listing (2), or the Itemized listing(3)-->Orientation = wdOrientLandscape
' paste the table into the newDoc and set some options
With newDoc
.ActiveWindow.View.ShowAll = False ' Hide all formatting marks
.ActiveWindow.View.ShowHiddenText = False ' Hide all hidden text
.Paragraphs(.Paragraphs.Count).Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=True
.Content.InsertParagraphAfter
.Range(.Content.End - 1).InsertBreak Type:=wdSectionBreakNextPage 'wdPageBreak
With .PageSetup
.Orientation = wdOrientLandscape
.RightMargin = Application.InchesToPoints(0.5)
.LeftMargin = Application.InchesToPoints(0.5)
End With
End With
' Autofit the table to the page
Set wordTbl = newDoc.Tables(newDoc.Tables.Count)
wordTbl.AutoFitBehavior (wdAutoFitWindow)
End Select