2015-05-27 76 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

哪個部分你看作是一個範圍?我不 ' –

相關問題