如何将活动Excel工作簿附加到电子邮件

我一直在尝试整个早上得到这个VBA脚本来附加我活跃的Excel文档到一个自动生成的Outlook消息。 如果我将文件path声明为string并附加它,一切工作正常。 除了我想附加当前的Excel文档的完整文件path,而不是使用静态string值。

这是我的代码:

Private Sub CommandButton1_Click() Dim OutApp As Object Dim OutMail As Object Dim strbody As String Dim sAttach As String Dim sTo As String Dim sCC As String 'For To field Set emailRng = Worksheets("Pre-Clearance Email").Range("E11:J14") For Each cl In emailRng sTo = sTo & ";" & cl.Value Next sTo = Mid(sTo, 2) 'For CC field Set emailRngCC = Worksheets("Pre-Clearance Email").Range("E16:J19") For Each cl In emailRngCC sCC = sCC & ";" & cl.Value Next sCC = Mid(sCC, 2) Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) 'variable declarations for email body and attachment strbody = "<BODY style=font-size:11pt;font-family:Calibri>Good Morning;<p>Please see the attached aliases for validation. Please let me know if you have any questions.<p>Thank you.</BODY>" sAttach = "K:\CRM Support\Data\Systematic Trade Recon (1).xlsm" 'the below code adds a users default signature to the email With OutMail .Display End With signature = OutMail.HTMLBody With OutMail .to = sTo .CC = sCC .Subject = "STR Pre-Clearance" .HTMLBody = strbody & signature .Attachments.Add (ActiveDocument.FullName) '.Attachments.Add sAttach .Display 'Instead of .Display, you can use .Send to send the email _ or .Save to save a copy in the drafts folder End With 

编译器在这一行给我一个错误:

 .Attachments.Add (ActiveDocument.FullName) 

我已经做了一些研究,并试图自己解决这个问题,但我无法弄清楚如何使这个脚本附加活动文件到这个Outlook邮件。 正如你可以看到我的代码,我的备份选项是只使用一个stringvariables和一个静态地址来附加文件,但我宁愿使这个脚本更多才多艺。

这里是我发现给我这个想法开始的网站之一: 在这里

好了,经过一番努力,我才得到了工作簿完美的附加。 这里是我对原始代码中OutMail对象的修改:

 With OutMail .to = sTo .CC = sCC .Subject = "STR Pre-Clearance" .HTMLBody = strbody & signature .Attachments.Add (ActiveDocument.FullName) 'this is the correction I made .Display 

我想我会回答我自己的问题,所以没有技术的答案,它不会stream连忘返。 也许这将有助于未来的人。

修复应该实际上是:

 With OutMail .To = sTo .CC = CC .Subject = "STR Pre-Clearance" .HTMLBody = strbody & signature .Attachments.Add (ActiveWorkbook.FullName) 'should be workbook not document .Display 'or .Send