【发布时间】:2018-05-15 11:53:12
【问题描述】:
我正在尝试在 Outlook 中编写一个宏,当我单击“新邮件”按钮时,它会提示输入附件。当我选择附件时,它会读取它的名称,并将该名称放在主题和正文中。
目前我能够执行上述任务,但有一些小问题,我希望能得到一些帮助。截至目前,当我被提示输入附件时,我选择了它,但它要求我第二次这样做。然后它将仅使用第二个附件作为信息并实际附加到电子邮件中。我的第二个问题是我无法弄清楚如何让电子邮件在编写宏时在末尾添加默认签名。
我在星期五之前从未使用过 VBA,而且编码经验很少,所以我希望有人能提供帮助。我从其他地方复制了一些代码,然后在其上构建,所以我知道它可能是混乱的,而不是最干净的代码。
Sub CreateNewMail()
Dim obApp As Object
Dim NewMail As MailItem
Dim otherObject As Word.Application
Dim fd As Office.FileDialog
Dim fileaddress As String
Dim filename As String
Dim signature As String
Set obApp = Outlook.Application
Set NewMail = obApp.CreateItem(olMailItem)
'Set to use Word for Attach File Dialog
Set otherObject = New Word.Application
otherObject.Visible = False
Set fd = otherObject.Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
.InitialFileName = "\\atro1\users\tdomanski\scan"
.Show
End With
fd.Show
fileaddress = fd.SelectedItems(1)
'Aquire File Name in correct form for Subject Line
Dim MidStart As Long
MidStart = InStrRev(fileaddress, "\") + 1
Dim MidEnd As Long
MidEnd = InStrRev(fileaddress, ".")
filename = Mid(fileaddress, MidStart, MidEnd - MidStart)
htmlbody1 = "<HTML><Body><p>Good Afternoon,</p><p>Please confirm receipt of attached "
htmlbody2 = "<br/>Please either email an order acknowledgement to me or initial & fax back PO to 716-655-0309.<p>Also, we are striving for 100% on-time delivery of purchase orders. If you cannot meet the required delivery date on the PO, please contact me as soon as possible.</p><p>Thank you!</p></body></html>"
'You can change the concrete info as per your needs
With NewMail
.Subject = filename
.BodyFormat = olFormatHTML
.HTMLBody = (htmlbody1 + filename + htmlbody2)
.Attachments.Add ((fileaddress))
.Display
End With
signature = oMail.HTMLBody
Set obApp = Nothing
Set NewMail = Nothing
End Sub
【问题讨论】:
-
请参阅this answer 了解如何添加签名。创建
NewMail后,如果您是NewMail.Display,Outlook 会填充签名。