使用.NET自动化Word - 单个横向页面

时间:2014-03-20 12:20:31

标签: vb.net word-automation

我试图将单个页面设置为横向,但是我尝试的每个方法都会更改整个文档,而不是我设置PageSetup的section \ paragraph。这是一个例子,第一页应该是肖像,第二页应该是横向:

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

1 个答案:

答案 0 :(得分:2)

您需要插入分页符,试试这个:

oPara1.Range.InsertBreak Type:=wdSectionBreakNextPage
wrdDataDoc.Sections(wrdDataDoc.Sections.Count).PageSetup.Orientation = wdOrientLandscape