VBA: change orientation to Landscape for word document converted from excel

BKChedlia

New Member
Joined
Jun 15, 2016
Messages
41
Dear all,

I have an excel file which I want to convert to word.

As the column number is big, the word document doesn't show all information, some of columns are not visible.

I changed manually the orientation to Landscape and I was able to see all data.

Issue VBA the change orientation to landscape not working.

This is my code:

Code:
Sub PasteToWord()     
    Dim wdApp As Object
    Dim wd As Object
     
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        Set wdApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
     
    Set wd = wdApp.Documents.Add
     
    wdApp.Visible = True
   Sheets("Sheet2").UsedRange.Font.Size = 8
Sheets("Sheet2").UsedRange.Copy
    wd.Range.Pasteandformat 0
   


wd.PageSetup.Orientation = wdOrientLandscape


wd.SaveAs ActiveWorkbook.Path & Application.PathSeparator & "Final_Report.docx"


    
End Sub

Can you please advice.
Thank you
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
You're referring to the orientation via wdOrientLandscape - which is a Word constant - but, unless you've set a reference to Word, the code won't know what to make of it. Since you're apparently using late binding (otherwise you wouldn't use 'Dim wdApp As Object') you need to either:
1. define wdOrientLandscape, like:
Const wdOrientLandscape as Long = 1
or
2. supply the constant's value explicitly, like:
wd.PageSetup.Orientation = 1 'wdOrientLandscape
 
Upvote 0

Forum statistics

Threads
1,214,407
Messages
6,119,332
Members
448,888
Latest member
Arle8907

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top