We help IT Professionals succeed at work.
Get Started

Export to Excel Page Setup Landscape

plpking1 asked
Last Modified: 2012-08-13
Exporting a query from Access to Excel using VB, trying to set the page to landscape
This is the code I am using:
oWS.PageSetup.Orientation = xlLandscape

This is the error that I receive:
Run-Time error  '1004'
Unable to set the Orientation property of the PageSetup Class

Running Access 2000, Excel 2000

Any help would be greatly appreciated.
Private Sub Command0_Click()
  On Error GoTo Err_Command0_Click
Dim rst As DAO.Recordset
Dim Path As String
Dim StrBU As String
Dim intDcode As String
Dim strQry As String
Dim StrExt As String
Dim strFile As String
Dim qDef As DAO.QueryDef
Dim oXL As Object
Dim oWB As Object
Dim oWS As Object
Set oXL = CreateObject("Excel.Application")
oXL.DisplayAlerts = False
Dim strContents
On Error Resume Next
Set qDef = CurrentDb.QueryDefs("Anniversary Report")
If Err.Number <> 0 Then Set qDef = CurrentDb.CreateQueryDef("Anniversary Report")
On Error GoTo 0
strQry = "SELECT Departments1.[DeptNum],Departments1.[RestNumbertext]FROM Departments1 ORDER BY Departments1.[DeptNum];"
Set rst = CurrentDb().OpenRecordset(strQry, dbOpenDynaset)
Do While Not rst.EOF
Path = "C:\Documents and Settings\Lisa\My Documents\Restaurant Monthly Reports\"
StrBU = rst("RestNumbertext")
StrExt = RestNumber & " - July Anniversary Report" & ".xls"
strFile = Path & StrBU & StrExt
intDcode = rst("DeptNum")
qDef.SQL = "SELECT * FROM QryAnniversaryReport WHERE DeptNum = " & intDcode & ""
DoCmd.OutputTo acOutputQuery, "Anniversary Report", acFormatXLS, strFile, False

Set oWB = oXL.Workbooks.Open(strFile)
Set oWS = oWB.Sheets(1)
oWS.Cells.Font.Name = "Times New Roman"
oWS.Cells.Font.Size = 10
oWS.Rows(1).Font.Bold = True
oWS.Columns("C").HorizontalAlignment = -4108
oWS.Columns("E").HorizontalAlignment = -4108
oWS.Columns("G:K").HorizontalAlignment = -4108
oWS.Columns("H").NumberFormat = "mm/dd/yyyy"
oWS.PageSetup.TopMargin = 0.2
oWS.PageSetup.RightMargin = 0.2
oWS.PageSetup.LeftMargin = 0.2
oWS.PageSetup.BottomMargin = 0.2
oWS.PageSetup.Orientation = xlLandscape
oWS.PageSetup.Zoom = False
oWS.PageSetup.FitToPagesWide = 1
oWS.PageSetup.FitToPagesTall = 1

Set oWB = Nothing

MsgBox ("Export Complete")
Set rst = Nothing
    Exit Sub
    MsgBox Err.Description
    Resume Exit_Command0_Click
End Sub

Open in new window

Watch Question
Expert of the Quarter 2010
Expert of the Year 2010
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE