将Access 2013select查询的结果导出到Excel 2013表单中,而不创build新的工作表或重新引用

这个问题相当简单,但是让我感到困惑,特别是因为这个代码在我创build的另一个应用程序中工作得非常好,所以我怀疑有一些隐藏的设置导致了问题。

我需要将访问select查询的结果转移到模板Excel工作表的副本中。 这个模板中有一些其他的表单,它们指向将要接收Access查询数据的表单,然后用它进行一些格式化和计算。 我所需要的只是将这个单个查询的结果传输到正确的表单中,而不用格式化或任何东西。 覆盖整张纸也很好。

我的问题是,而不是复制到工作表“qryLineItemsExportBuffer”,而是创build一个名为“qryLineItemsExportBuffer1”的新工作表,并将查询结果 – 所以在工作簿中的所有引用查找错误的地方。 我也不能使用DoCmd.TransferSpreadsheet [Range]参数来定义目标工作表, 因为这只在导出时不起作用。

这是我的代码 – 它1.打开模板,2.保存一个新名称的副本,3.传输查询结果,然后4.打开副本,并重新计算/重build链接:

Private Sub cmdExportInvoiceRequestForm_Click() Dim instExcelTemplate As Object Dim xlsxInvoiceRequestTemplate As Excel.Workbook Dim xlsxInvoiceRequestNewName As String Dim xlsxInvoiceRequestCopy As Excel.Workbook xlsxInvoiceRequestNewName = Application.CurrentProject.Path & "\" & CustomerName & "-InvoiceRequest-" & Format(Date, "dd-mm-yyyy") & ".xlsx" 'create the name for the new invoice report MsgBox xlsxInvoiceRequestNewName 'user is shown the name and date MsgBox Application.CurrentProject.Path 'user is shown the destination folder 'start an Excel instance and open the template workbook. Set instExcelTemplate = CreateObject("Excel.Application") Set xlsxInvoiceRequestTemplate = instExcelTemplate.Workbooks.Open(Application.CurrentProject.Path & "\Templates\CustomerInvoiceRequestFormTemplate.xlsx") 'open the invoice report template xlsxInvoiceRequestTemplate.SaveAs FileName:=(xlsxInvoiceRequestNewName) 'save the template as a new file in the correct directory instExcelTemplate.Workbooks.Close 'close the Excel instance. 'transfer the qryLineItemsExportBuffer results to the newly created workbook. DoCmd.TransferSpreadsheet acExport, 10, "qryLineItemsExportBuffer", xlsxInvoiceRequestNewName 'start an excel instance and open the new workbook. Set instExcelCopy = CreateObject("Excel.Application") Set xlsxInvoiceRequestCopy = instExcelCopy.Workbooks.Open(xlsxInvoiceRequestNewName) instExcelCopy.CalculateFullRebuild 'recalculate all values and rebuild links in the workbook. This is necessary because it does not do this when opened manually. Can be done in Excel with CTRL+SHIFT+ALT+F9. xlsxInvoiceRequestCopy.Save 'save the copy instExcelCopy.Workbooks.Close 'close the Excel instance. Set xlsxInvoiceRequestCopy = Nothing Set instExcelCopy = Nothing xlsxInvoiceRequestNewName = "" End Sub 

我也不能把任何代码放入Excel模板本身,所以我不能把它从新工作表复制。

我错过了什么?

我find了正确的方法来使用DoCmd.TransferSpreadsheet方法将Access查询的结果导出到Excel工作表中的特定工作表或范围,而无需创build新工作表。 此方法的文档强烈build议您不能设置从Access导出到Excel的范围参数,我还没有发现任何其他联机提到此function的来源。 但实际上,您可以使用Excel的名称pipe理器来有效地设置DoCmd.TransferSpreadsheet的目标范围。

假设我们要将Access查询“qryItemsExport”的结果传输到名为“DestinationWorkbook.xlsx”的工作簿中与Access数据库相同的文件夹中。

首先,进入工作簿的名称pipe理器,并设置一个名为“qryItemsExport”的命名范围,以便数据进入。 然后,在Access项目中使用下面的VBA:

 Dim xlsxDestinationWorkbook as String xlsxDestinationWorkbook = Application.CurrentProject.Path & "\" & "DestinationWorkbook.xlsx" DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qryItemsExport", xlsxDestinationWorkbook 

第一行将工作簿名称variables声明为一个string。 第二行将工作簿名称variables分配给与Access项目位于同一文件夹中的版本。 第三行将数据从查询导出到目标工作簿,Excel 2010+格式。 由于该命令尝试导出到目标“qryItemsExport”,因此会转到先前命名的范围,并将数据放在该位置,从左上angular开始。 它不会创build新工作表或重命名现有工作表,因此对正在复制的工作表的引用将保持不变。