Question

I'm trying to set a single page to be landscape, however every method I try changes the entire document rather than the section\paragraph I set PageSetup on. Here's an example, the first page should be portrait and the second should be landscape:

Dim wrdApp As Word.Application
Dim wrdDoc As Word._Document
Public Sub test()
    Dim wrdSelection As Word.Selection
    Dim wrdDataDoc As Word._Document
    Dim sText As String

    wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True

    wrdDataDoc = wrdApp.Documents.Open("C:\Temp\Doc1.docx")
    wrdDataDoc.PageSetup.Orientation = WdOrientation.wdOrientPortrait

    Dim oPara1 As Paragraph
    sText = "Test Report Title"
    oPara1 = wrdDataDoc.Content.Paragraphs.Add
    oPara1.Range.Text = sText
    oPara1.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter
    oPara1.Range.InsertParagraphAfter()

    Dim para As Word.Paragraph = wrdDataDoc.Paragraphs.Add()
    para.Range.InsertBreak()

    wrdApp.Selection.GoTo(Word.WdGoToItem.wdGoToLine, Word.WdGoToDirection.wdGoToLast)

    wrdDataDoc.Sections(1).PageSetup.Orientation = WdOrientation.wdOrientLandscape


    wrdSelection = wrdApp.Selection()
    wrdDataDoc.Tables.Add(wrdSelection.Range, NumRows:=9, NumColumns:=4)

    With wrdDataDoc.Tables.Item(1)
        'Code for table here
    End With

End Sub
Was it helpful?

Solution

You need to insert a page break, try this:

oPara1.Range.InsertBreak Type:=wdSectionBreakNextPage
wrdDataDoc.Sections(wrdDataDoc.Sections.Count).PageSetup.Orientation = wdOrientLandscape
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top