简体   繁体   中英

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. 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. 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:

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.

There are two possibilities after using DoCmd.OutputTo or DoCmd.TransferSpreadsheet . This can all be done with 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.
    Use the Excel macro recorder to give you a head start.

It depends on the complexity of your template, which one is better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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