我正在创建一个表单,该表单从查询中生成一个表,并将数据分散到excel文件中的7个数据表中。我遇到的问题是极端不一致。此错误并不总是出现,并且当它出现在7个导出中的一个中时。
我的代码:
Private Sub cmdExport_Click()
Dim xl As Object
'Step 1: Start Excel, then open the target workbook.
Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open (CurrentProject.Path & "\" & "Report.xlsm")
'Step 2: Make Excel visible
xl.Visible = True
xl.ActiveWorkbook.Activate
'Step 3: Run the target macro
xl.Run "PreImport"
' xl.ActiveWorkbook.Save
'Step 4: Close and save the workbook, then close Excel
xl.ActiveWorkbook.Close
xl.Quit
'Step 5: Memory Clean up.
Set xl = Nothing
' The TransferSpreadsheet command below this comment is where the errors are occuring
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_1", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_1"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_2", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_2"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_3", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_3"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_4", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_4"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_5", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_5"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_6", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_6"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_7", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_7"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_8", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_8"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_9", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_9"
' The TransferSpreadsheet command above this comment is where the errors are occuring
Dim xlapp As Excel.Application
Set xlapp = CreateObject("Excel.Application")
xlapp.Visible = True
xlapp.Workbooks.Open CurrentProject.Path & "\" & "Report.xlsm", True, False
Set xlapp = Nothing
End Sub
宏" PreImport"正在运行的内容如下:
WORKSHEET_NAME_1
到WORKSHEET_NAME_9
主要代码(如上所示)应该是:
PreImport
宏Report.xlsm
答案 0 :(得分:0)
我不确定你是否有正确的论点:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_2", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_2"
好像你应该使用acSpreadsheetTypeExcel12
代替acSpreadsheetTypeExcel9
。但更重要的是,根据MSDN,Range
字段应留空:
[MSDN]范围 - (...)导出到电子表格时,必须将此参数留空。 如果输入范围,导出将失败。(...)