首页 > 解决方案 > 如何使用 VBA Excel 引用自动格式化回复电子邮件中的范围?

问题描述

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

在正文字段中,我希望引用位于原始 Excel 工作表中 Range("A6") 中的 "VOLUME:" 之后的值。每次我更改 Range("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

标签: htmlexcelvbaemailhyperlink

解决方案


假设您想要 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>"

推荐阅读