发送电子邮件附件

我有两个Excel文件:一个是启用macros,一个是空白的Excel表单。

场景: – 目前,空白的Excel表格将被手动填写,并附在Lotus Notes电子邮件中,并每天向外发送。 – 这个Excel空白表格一旦被填充信息,也会保存为具有不同文件名称的新Excel文件。

现在,我想编写VBA Excel,只需要点击button发送附在Lotus Notes电子邮件中的Excel表单。

我已经find了代码如下,它正在工作:

Sub Send_Email_via_Lotus_Notes() Dim Maildb As Object Dim MailDoc As Object Dim Body As Object Dim Session As Object 'Start a session of Lotus Notes Set Session = CreateObject("Lotus.NotesSession") 'This line prompts for password of current ID noted in Notes.INI Call Session.Initialize 'or use below to provide password of the current ID (to avoid Password prompt) 'Call Session.Initialize("<password>") 'Open the Mail Database of your Lotus Notes Set Maildb = Session.GETDATABASE("", "D:\Notes\data\Mail\eXceLiTems.nsf") If Not Maildb.IsOpen = True Then Call Maildb.Open 'Create the Mail Document Set MailDoc = Maildb.CREATEDOCUMENT Call MailDoc.REPLACEITEMVALUE("Form", "Memo") 'Set the Recipient of the mail Call MailDoc.REPLACEITEMVALUE("SendTo", "Ashish Jain") 'Set subject of the mail Call MailDoc.REPLACEITEMVALUE("Subject", "Subject Text") 'Create and set the Body content of the mail Set Body = MailDoc.CREATERICHTEXTITEM("Body") Call Body.APPENDTEXT("Body text here") 'Example to create an attachment (optional) Call Body.ADDNEWLINE(2) Call Body.EMBEDOBJECT(1454, "", "C:\dummy.txt", "Attachment") 'Example to save the message (optional) in Sent items MailDoc.SAVEMESSAGEONSEND = True 'Send the document 'Gets the mail to appear in the Sent items folder Call MailDoc.REPLACEITEMVALUE("PostedDate", Now()) Call MailDoc.SEND(False) 'Clean Up the Object variables - Recover memory Set Maildb = Nothing Set MailDoc = Nothing Set Body = Nothing Set Session = Nothing 

但是,我想附件文件名和电子邮件主题是可变的,而不是硬编码一个静态文件名和path。

请有人提供一些指导?

使用参数。

 Sub Send_Email_via_Lotus_Notes(filename as string, emailSubject as string) Dim Maildb As Object Dim MailDoc As Object Dim Body As Object Dim Session As Object 'Start a session of Lotus Notes Set Session = CreateObject("Lotus.NotesSession") 'This line prompts for password of current ID noted in Notes.INI Call Session.Initialize 'or use below to provide password of the current ID (to avoid Password prompt) 'Call Session.Initialize("<password>") 'Open the Mail Database of your Lotus Notes Set Maildb = Session.GETDATABASE("", "D:\Notes\data\Mail\eXceLiTems.nsf") If Not Maildb.IsOpen = True Then Call Maildb.Open 'Create the Mail Document Set MailDoc = Maildb.CREATEDOCUMENT Call MailDoc.REPLACEITEMVALUE("Form", "Memo") 'Set the Recipient of the mail Call MailDoc.REPLACEITEMVALUE("SendTo", "Ashish Jain") 'Set subject of the mail Call MailDoc.REPLACEITEMVALUE("Subject", emailSubject) 'Create and set the Body content of the mail Set Body = MailDoc.CREATERICHTEXTITEM("Body") Call Body.APPENDTEXT("Body text here") 'Example to create an attachment (optional) Call Body.ADDNEWLINE(2) Call Body.EMBEDOBJECT(1454, "", filename, "Attachment") 'Example to save the message (optional) in Sent items MailDoc.SAVEMESSAGEONSEND = True 'Send the document 'Gets the mail to appear in the Sent items folder Call MailDoc.REPLACEITEMVALUE("PostedDate", Now()) Call MailDoc.SEND(False) 'Clean Up the Object variables - Recover memory Set Maildb = Nothing Set MailDoc = Nothing Set Body = Nothing Set Session = Nothing