gpt4 book ai didi

excel - 如何将事件 Excel 工作簿附加到电子邮件

转载 作者:行者123 更新时间:2023-12-02 13:04:42 25 4
gpt4 key购买 nike

我整个上午都在尝试获取此 VBA 脚本,将我的事件 Excel 文档附加到自动生成的 Outlook 消息中。如果我将文件路径声明为字符串并附加它,一切都会正常工作。除了我想附加当前 Excel 文档的完整文件路径而不是使用静态字符串值。

这是我的代码:

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 消息中。正如您从我的代码中看到的,我的备份选项是仅使用字符串变量和静态地址来附加文件,但我宁愿让这个脚本比这更通用。

这是我发现给我这个想法的网站之一:Here

最佳答案

好吧,经过更多的努力,我能够完美地附加工作簿。以下是我对原始代码中的 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

我想我应该回答我自己的问题,这样它就不会在没有技术答案的情况下徘徊。也许它会对将来的某人有所帮助。

关于excel - 如何将事件 Excel 工作簿附加到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22255655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com