vba Word 文档:设置为横向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13242369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:21:54  来源:igfitidea点击:

Word document : Set to landscape

vbams-wordword-vba

提问by ApplePie

I have this ridiculous problem on ms-word2007. I have most of my macro working as intended but the orientation can't seem to stay in place. I set it to landscape using VBA but it will always go back to portrait. If I step into the code right after this line the document ISin landscape but as soon as I click even once only in the document it goes back to portrait.

我在ms-word2007年遇到了这个荒谬的问题。我的大部分宏都按预期工作,但方向似乎无法保持原位。我使用 VBA 将其设置为横向,但它总是会返回纵向。如果我在此行之后立即进入代码,文档横向的,但只要我在文档中单击一次,它就会返回纵向。

Do you guys have an idea why this happens ? I can't seem to find anyone having this bug on Google.

你们知道为什么会这样吗?我似乎无法在 Google 上找到任何有此错误的人。

Option Explicit

Sub créer_rapport()
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document

    Set wdApp = New Word.Application
    wdApp.Visible = True

    Set wdDoc = wdApp.Documents.Open(Range("path_fichier").Value)

    wdApp.Selection.WholeStory
    wdApp.Selection.Font.Name = "Courier New"
    wdApp.Selection.Font.Size = 7


    wdDoc.PageSetup.Orientation = wdOrientLandscape
    wdDoc.PageSetup.PaperSize = wdPaperLegal
    wdDoc.SaveAs ActiveWorkbook.Path & "\test2", wdFormatXMLDocument

    Do While wdApp.Selection.Find.Execute("Merge")
        wdApp.Selection.MoveUp wdLine, 1
        wdApp.Selection.InsertBreak wdPageBreak
        wdApp.Selection.MoveDown wdLine, 2
    Loop

    With wdDoc
        .SaveAs (ActiveWorkbook.Path & "\test")
        .Close (True)
    End With

    wdApp.Quit False
End Sub

This is all there is to my macro (for now).

这就是我的宏(目前)的全部内容。

(Oh and you can highlight bad style, this is the first time I do VBA macros for Word (I do them all the time in Excel))

(哦,你可以突出糟糕的风格,这是我第一次为 Word 做 VBA 宏(我一直在 Excel 中做))

Thanks !

谢谢 !

回答by Denys Wessels

Instead of:

代替:

wdDoc.PageSetup.Orientation = wdOrientLandscape

Try this:

尝试这个:

Selection.PageSetup.Orientation = wdOrientLandscape