2015-05-27 40 views
0

我正在创建一个表格,该表格从查询生成一个表格并将数据分散到Excel文件中的7个数据表中。我遇到的问题极其不一致。这个错误并不总是出现,当它出现在7个输出中的一个时。外部表格不是预期的格式(Access 2010 VBA)

我的代码:

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 

宏 “预导入” 正被RAN执行以下操作:

  1. 拷贝/粘贴2列到新的位置(显示上周的数据)。
  2. 删除从WORKSHEET_NAME_1所有的工作表WORKSHEET_NAME_9
  3. 保存文件

主代码(如上所示)被认为:

  1. 运行PreImport
  2. 转储数据到Report.xlsm
  3. 保存文件

回答

0

我不知道你有合适的参数:

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "QUERY_NAME_2", CurrentProject.Path & "\" & "Report.xlsm", True, "WORKSHEET_NAME_2" 

好像你应该使用acSpreadsheetTypeExcel12代替acSpreadsheetTypeExcel9。但更重要的是根据MSDNRange字段应留空为:

[MSDN]范围 - (...)当您导出到电子表格,则必须将该参数留空。 (...)

+0

哪个部分你看作是一个范围?我不 ' –

相关问题