2013-03-04 104 views
0

林创建Excel文件得到错误无法从MS Access 2007

Object type argument for the action or method is blank or invalid 

真的不明白,为什么作为代码即时通讯使用的作品在另一个数据库中。这里的代码:

'Print the report 
Private Sub printReport() 

     Dim myReport As String 
     Dim oXL As Excel.Application 
     Dim oBook As Excel.Workbook 
     Dim oSheet As Object 

     DoCmd.SetWarnings False 

     myReport = "\Desktop" & "Report_" & (Format(Now(), "yyyymmdd")) & ".xls" 

     If GetVersion = "11.0" Then 
      DoCmd.OutputTo acOutputQuery, qryTest, acSpreadsheetTypeExcel9, myReport 

     ElseIf GetVersion = "12.0" Then 
      DoCmd.OutputTo acOutputQuery, qryTest, acSpreadsheetTypeExcel12, myReport 

     Else 
      DoCmd.OutputTo acOutputQuery, qryTest, "Excel 97 - Excel 2003 Workbook (*.xls)", myReport 

     End If 

     Set oXL = New Excel.Application 
     oXL.Visible = True 
     Set oBook = oXL.Workbooks.Open(myReport) 
     Set oSheet = oXL.ActiveSheet 

     With oBook.ActiveSheet.PageSetup 
      .CenterHeader = "Hep C New Patient Report " & (Format(Now(), "m/d/yyyy")) 
      .CenterFooter = "&P" 
      .PrintTitleRows = "$1:$1" 
      .PrintTitleColumns = "$A:$A" 
      .Orientation = 2 '1 = Portrait, 2 = Landscape 
      .LeftMargin = 18 '72 = 1 inch 
      .RightMargin = 18 
      .TopMargin = 54 
      .BottomMargin = 54 
      .PrintGridlines = True 
      .PaperSize = 1  '5 = Legal, 1 = Letter 
      .ORDER = 2   '2 = Over then down, 1 = Down then over 
     End With 

     oSheet.Cells.Select 
     oSheet.Cells.EntireColumn.AutoFit 
     oBook.Save 

     oXL.Visible = False 

     If (MsgBox("Is it OK to close the report and return to CPE Application?", vbYesNo)) = vbYes Then 
      Set oBook = Nothing 
      Set oSheet = Nothing 
      oXL.Quit 
      Set oXL = Nothing 
     Else 
      MsgBox ("Close Excel when finished to return here.") 
      oXL.Visible = True 
     End If 

     DoCmd.SetWarnings True 

End Sub 

任何想法?

P.S.它表示错误是由Else语句代码生成的

+0

还有哪些?你有2。 – 2013-03-04 18:21:15

+0

错误是由“Excel 97 - Excel 2003工作簿(* .xls)”引发的,我想。你想用这条线达到什么目的?如果你想要一个2003文件格式的.xls文件,你可以试试acFormatXLS。 – Zaider 2013-03-04 19:10:05

+0

它在执行“Excel 97 - Excel 2003工作簿”的else语句中, – Katana24 2013-03-04 21:26:44

回答

0

获取错误的原因是因为qryTest(它用于填充Excel表单的查询)不在引号中,即“qryTest”。这是必要的