我对 VBA 比较陌生,我正在尝试将一系列单元格导出到 Outlook 以通过电子邮件发送。我希望运行宏的人将其默认签名粘贴到范围下方,但是当运行我的代码时,它将范围粘贴到签名下方而不是上方。
我尝试使用 .HTMLBody 函数在 Outlook 中创建默认签名,但无法将其放置在正确的位置。
理想情况下,我会添加初始 htmlbody 文本,然后粘贴范围,然后使用 & .htmlbody 添加默认签名,但我不能这样做。
我尝试过其他选择,例如从 .htm 文件或 .rtf 文件调用签名,但这不符合我的目的,因为我需要自动输入运行宏的签名。
如有任何帮助或建议,我们将不胜感激。
这是我的代码的摘录
'Get the Active instance of Outlook if there is one
Set oLookApp = GetObject(, "Outlook.Application")
'If Outlook isn't open then create a new instance of Outlook
If Err.Number = 429 Then
'Clear Error
Err.Clear
'Create a new instance of Outlook
Set oLookApp = New Outlook.Application
End If
'Create a new email
Set oLookItm = oLookApp.CreateItem(olMailItem)
Set CopyRange1 = ThisWorkbook.Worksheets("TEST EMAILS").Range("Z2").CurrentRegion
'Create an array to hold ranges
With oLookItm
'Define some basic info of our email
.To = [email protected]
.CC = [email protected]
.BCC = EmailAddresses
.Subject = "Here are all of my Prices"
.Display
.HTMLBody = "<span style='background:yellow;mso-highlight:yellow'>" & "SENSITIVE INFORMATION" & "<a href="SENSITIVE INFORMATION"><u><b>SENSITIVE INFORMATION </a></u></b></span><br><br>" & "<img src='C:\Users\User\Pictures\Picture1.png'><br>" & "SENSITIVE INFORMATION<br>" & "SENSITIVE INFORMATION &" & "<b><u> SENSITIVE INFORMATION </b></u>" & "SENSITIVE INFORMATION<br>" & "SENSITIVE INFORMATION" & "<b><font color=red> SENSITIVE INFORMATION </font></b>" & "SENSITIVE INFORMATION<br>" & "<b>SENSITIVE INFORMATION</b>" & .HTMLBody
'Display the email
'Get the Active Inspector
Set oLookIns = .GetInspector
'Get the document within the inspector
Set oWrdDoc = oLookIns.WordEditor
CopyRange1.Copy
'Define the range, insert a blank line, collapse the selection.
Set oWrdRng = oWrdDoc.Application.ActiveDocument.Content
oWrdRng.Collapse Direction:=wdCollapseEnd
'Add a new paragragp and then a break
Set oWrdRng = oWdEditor.Paragraphs.Add
oWrdRng.InsertBreak
'Paste the object.
oWrdRng.PasteSpecial DataType:=wdPasteHTML
CopyRange1.Delete
End With
Unload UserForm2 here
如果需要更多信息,请告诉我
这对我有用 - 用 Excel 中粘贴的范围替换内容中的占位符:
'add references to Word and Outlook object models...
Sub Tester77()
Const TABLE_PLACEHOLDER As String = "XXXtableXXX"
Dim oLookApp As Outlook.Application, oLookItm As Outlook.MailItem
Dim oLookIns As Outlook.Inspector, oWrdDoc As Word.Document, oWrdRng As Word.Range, CopyRange1 As Range
Set CopyRange1 = ThisWorkbook.Worksheets("TEST EMAILS").Range("Z2").CurrentRegion
On Error Resume Next
Set oLookApp = GetObject(, "Outlook.Application")
On Error GoTo 0
If oLookApp Is Nothing Then Set oLookApp = New Outlook.Application
With oLookApp.CreateItem(olMailItem)
'.To = [email protected]
'.CC = [email protected]
'.BCC = EmailAddresses
.Subject = "Here are all of my Prices"
.Display
.HTMLBody = "<span style='background:yellow;mso-highlight:yellow'>" & _
"SENSITIVE INFORMATION" & "<a href=""SENSITIVE INFORMATION""><u>" & _
"<b>SENSITIVE INFORMATION </a></u></b></span><br><br>" & _
"<img src='C:\Users\User\Pictures\Picture1.png'><br>" & _
"SENSITIVE INFORMATION<br>" & "SENSITIVE INFORMATION " & _
"<b><u> SENSITIVE INFORMATION </b></u>" & _
"SENSITIVE INFORMATION<br>" & "SENSITIVE INFORMATION" & _
"<b><font color=red> SENSITIVE INFORMATION </font></b>" & _
"SENSITIVE INFORMATION<br>" & "<b>SENSITIVE INFORMATION</b><br><br>" & _
TABLE_PLACEHOLDER & .HTMLBody
Set oLookIns = .GetInspector
Set oWrdDoc = oLookIns.WordEditor
Set oWrdRng = oWrdDoc.Application.ActiveDocument.Content
'find the table placeholder and paste the Excel range
With oWrdRng.Find
.Text = TABLE_PLACEHOLDER
If .Execute Then
CopyRange1.Copy
'If the `Find` succeeded then `oWrdRng` is
' now pointing to the range for the found text
oWrdRng.PasteSpecial DataType:=wdPasteHTML
End If
End With
End With
End Sub
有问题的代码行如下:
.HTMLBody = "<span style='background:yellow;mso-highlight:yellow'>" & "SENSITIVE INFORMATION" & "<a href="SENSITIVE INFORMATION"><u><b>SENSITIVE INFORMATION </a></u></b></span><br><br>" & "<img src='C:\Users\User\Pictures\Picture1.png'><br>" & "SENSITIVE INFORMATION<br>" & "SENSITIVE INFORMATION &" & "<b><u> SENSITIVE INFORMATION </b></u>" & "SENSITIVE INFORMATION<br>" & "SENSITIVE INFORMATION" & "<b><font color=red> SENSITIVE INFORMATION </font></b>" & "SENSITIVE INFORMATION<br>" & "<b>SENSITIVE INFORMATION</b>" & .HTMLBody
当您需要在自动添加的签名之前添加某些内容时,您需要在邮件正文的 HTML 标记中找到开始
<body>
标记,然后将 HTML 代码段粘贴到找到的 HTML 标记后面。
使用 VBA 中可用的 InStr 函数获取
<body>
标记的位置,然后使用 Replace 函数 将 body
标记替换为您的内容(当然还有 body 标记以保持标记良好) -形成)。