We help IT Professionals succeed at work.
Get Started

Export to Excel Page Setup Landscape

plpking1
plpking1 asked
on
753 Views
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
 
Err.Clear
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)
rst.MoveLast
rst.MoveFirst
 
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("A:Z").AutoFit
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

oWB.Save
oWB.Close
Set oWB = Nothing

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

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Expert of the Quarter 2010
Expert of the Year 2010
Commented:
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