简体   繁体   中英

How can I change objWord Page Orientation from Excel VBA

I'm working on a project were I run a report and when the work is done I want Excel to select and copy a range then paste as picture in a new Word document.

As the size of the image is relatively big for a portrait orientation, I want Word to change it to Landscape mode. And that's my problem. Can someone help?

Sub PasteAsPicture()
    Dim objWord, objDoc As Object
    
    With Workbooks("Workbook Name").Sheets("Sheet Name")
        .Range("A1:S40").CopyPicture Appearance:=xlScreen, Format:=xlPicture
    End With
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add
    With objWord
        .Visible = True
        .ActiveDocument.PageSetup.Orientation = wdOrientLandscape
        .Selection.Paste
        .Selection.TypeParagraph
    End With
End Sub

Without a reference to the Word object library, the constant wdOrientLandscape will have no value in Excel VBA, so either:

  • add the Word reference
  • define the constant in your Excel VBA
  • or substitute the numeric value (1 in this case).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM