外部表不是预期的格式(Access 2010 VBA)

时间:2015-05-27 12:03:51

标签: excel vba ms-access-2010

我正在创建一个表单,该表单从查询中生成一个表,并将数据分散到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"正在运行的内容如下:

  1. 将2列复制/粘贴到新位置(显示上周的数据)。
  2. 删除从WORKSHEET_NAME_1WORKSHEET_NAME_9
  3. 的所有工作表
  4. 保存文件
  5. 主要代码(如上所示)应该是:

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

1 个答案:

答案 0 :(得分:0)

我不确定你是否有正确的论点:

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

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

  

[MSDN]范围 - (...)导出到电子表格时,必须将此参数留空。   如果输入范围,导出将失败。(...)