这个问题相当简单,但它让我感到困惑,特别是因为这段代码在我开发的不同的应用程序中工作得非常好,所以我怀疑有什么隐藏的设置导致了问题。
我需要将Access select查询的结果传输到模板Excel表的副本中。此模板中有其他几个工作表,它们引用将接收访问查询数据的工作表,然后该工作表将对其进行一些格式化和计算。我所需要的只是将这个查询的结果转移到正确的工作表中,不需要格式化或任何内容。把整张纸都覆盖也可以。
我的问题是,它没有复制到工作表"qryLineItemsExportBuffer",而是创建了一个名为"qryLineItemsExportBuffer1“的新工作表,并将查询结果放在那里--因此工作簿中的所有引用都找错了位置。我也不能使用DoCmd.TransferSpreadsheet Range参数来定义目标表因为这在导出时不起作用,只能在导入时起作用。
下面是我的代码-它1.打开模板,2.保存带有新名称的副本,3.传递查询结果,4.打开副本并重新计算/重新构建链接:
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模板本身,所以我不能让它从新的工作表中复制数据。
我遗漏了什么?
发布于 2017-01-25 23:09:42
我找到了正确的方法,可以使用DoCmd.TransferSpreadsheet方法将访问查询的结果导出到Excel工作表中的特定工作表或范围,而无需创建新的工作表。此方法的文档强烈建议您不能设置从Access导出到Excel的范围参数,而且我还没有在网上找到任何提及此功能的其他来源。但是实际上,通过使用DoCmd.TransferSpreadsheet的名称管理器,您可以有效地设置一个目标范围。
假设我们希望将访问查询'qryItemsExport‘的结果传输到与Access数据库相同的文件夹中的工作簿中的一个工作表'DestinationWorkbook.xlsx’。
首先,进入工作簿的名称管理器,并设置一个名为“qryItemsExport”的命名范围,您希望在其中获取数据。然后,在Access项目中使用以下VBA:
Dim xlsxDestinationWorkbook as String
xlsxDestinationWorkbook = Application.CurrentProject.Path & "\" & "DestinationWorkbook.xlsx"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qryItemsExport", xlsxDestinationWorkbook第一行将工作簿名称变量声明为字符串。第二行将工作簿名称变量分配给当前与Access项目位于同一文件夹中的版本。第三行将数据从查询导出到目标工作簿,以获得Excel 2010+格式。由于命令试图导出到目的地'qryItemsExport',这将转到先前命名的范围,并将数据放在该位置,从左上角开始。它不会创建新的工作表或重命名现有的工作表,因此对要复制的工作表的引用将保持不变。
https://stackoverflow.com/questions/41860933
复制相似问题