简体   繁体   English

从Access导出查询到Excel模板

[英]Export Query from Access to an Excel Template

I have a query that runs on a specified day of the week. 我有一个查询,它在一周的指定日期运行。 On that day if any record is marked "Needs Ordered" then an orderform window will open with a command button. 在这一天,如果任何记录都标记为“需要订购”,则订单窗体窗口将打开并带有命令按钮。 The command button should export the query data to a excel template (already formatted the way it should look) and then attach to an email template to send. 命令按钮应将查询数据导出到excel模板(已经格式化为外观),然后附加到电子邮件模板进行发送。 I can get the query to attach to the email I just can't seem to figure out how to get it to use the excel template instead of creating a new excel workbook each time. 我可以将查询附加到电子邮件,但似乎无法弄清楚如何使用excel模板而不是每次创建新的excel工作簿。 Any suggestions on what to try? 有什么建议可以尝试吗?

Dim myolapp As Object 
Dim myitem As Object 

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "BoxOrder", "C:\Users\Database Design\Desktop\BoxOrder.xlsx", True 

Set myolapp = CreateObject("Outlook.Application") myolapp.Session.Logon 
Set myitem = myolapp.CreateItemFromTemplate("F:\BOX ORDER FOR PC CONNECTION.oft") 
Set myAttachments = myitem.attachments 

myAttachments.Add "C:\Users\Database Design\Desktop\BoxOrder.xlsx"
myitem.Display 'or send 
DoCmd.Close acForm, "OrderForm"

The BoxOrder query: BoxOrder查询:

SELECT '' AS [Quantity Needed], [BoxOrder].[Box Size], [BoxOrder].Comments, 
       [BoxOrder].[Certified/Stamped Boxes], [BoxOrder].[Box Type] 
FROM BoxOrder; 

You can't. 你不能 The TemplateFile parameter of DoCmd.OutputTo is only for HTML (and similar) export. DoCmd.OutputToTemplateFile参数仅用于HTML(及类似)导出。

There are two possibilities after using DoCmd.OutputTo or DoCmd.TransferSpreadsheet . 使用DoCmd.OutputToDoCmd.TransferSpreadsheet之后,有两种可能性。 This can all be done with VBA: 这一切都可以通过VBA完成:

  • Open exported file and template file, copy & paste the data from export to template, save as new file. 打开导出的文件和模板文件,将导出的数据复制并粘贴到模板,另存为新文件。
  • Open exported file, apply the formatting with VBA. 打开导出的文件,使用VBA进行格式化。
    Use the Excel macro recorder to give you a head start. 使用Excel宏记录器可以帮助您起步。

It depends on the complexity of your template, which one is better. 这取决于模板的复杂程度,哪个更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM