如何使用VBA Excel在自动格式化的回复电子邮件中引用范围?



我有下面的代码。因为我正在使用.HTMLBody 我需要在 VBA 中使用 HTML 标记。电子邮件的收件人将在电子邮件正文中看到一个可点击的值(通过代码中的"AHREF"的超链接)。单击后,将打开新撰写的电子邮件,其中在"收件人","主题"和"正文"字段中包含一些基本的预填充文本。

在"正文"字段中,我希望引用"VOLUME:"之后的值,该值放置在原始Excel工作表中的Range(" A6")中。每次我更改范围("A6")中的值时,都会再次发送电子邮件。收件人将再次单击超链接,并且应该看到与上一封电子邮件相比更改的值。这可能吗?

Sub Test()
Dim oApp As Object
Dim oEmail As Object

Dim Header As String
Dim Alpha As String
Dim olkPA As Outlook.PropertyAccessor
Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001F"
Header = "<html>" & "<table cellpadding= 5>" & "<tr bgcolor=""#000080"">" 
& "<font color =""white"";font face =Calibri><b>" & "<td width = 250>" & 
Range("A5") & "</td>" & _
"<td align=""center"";td width= 60>" & Range("E5") & "</td>" & "</font> 
</b>" & "</tr>" & "</html>"
Alpha = "<tr bgcolor=""#F0F0F0"">" & "<font face =Calibri>" & "<td>" & 
Range("A6") & "</td>" & "<td align=""center"">" & "<A HREF='mailto: 
xxx@xxx.com&subject=***ENQUIRY***&Body=INSTRUCTION: EXTEND %0D%0DVOLUME: 
1000  %0D%0DCODE: 12345 '>" & _
"<font color =""blue"">" & Range("E6") & "</td>" & "</A>" & "</font>" 
& "</tr>" & "</html>"
Set oApp = CreateObject("Outlook.Application")
Set oEmail = oApp.CreateItem(0)
Set oApp = CreateObject("Outlook.Application")
Set oEmail = oApp.CreateItem(olMailItem)
With oEmail
.To = ""
.CC = ""
.BCC = "xxx@xxx.com"
.Subject = "Test"
.HTMLBody = Header & Alpha
.Display
End With
Set oEmail = Nothing
Set oApp = Nothing
Set colAttach = Nothing
Set oAttach = Nothing
End Sub

假设您希望 A6 中的值代替当前存在的 1000,您应该能够使用对 Range("A6") 的引用来构造超链接字符串,就像在整个 html 字符串的其他部分中一样:

Alpha = "<tr bgcolor=""#F0F0F0"">" & "<font face =Calibri>" & "<td>" & Range("A6") & "</td>" & "<td align=""center"">" & _
"<A HREF='mailto: xxx@xxx.com&subject=***ENQUIRY***&Body=INSTRUCTION: EXTEND %0D%0DVOLUME: " & Range("A6") & " %0D%0DCODE: 12345 '>" & _
"<font color =""blue"">" & Range("E6") & "</td>" & "</A>" & "</font>" & "</tr>" & "</html>"

最新更新