Vba 复制并转发正文中包含图像的电子邮件
我试图复制电子邮件的正文,并在用户转发之前将其放入模板中 原始电子邮件正文中的图像变成空白框,其中包含红色X 错误消息: 无法显示链接的图像。文件可能已被移动、重命名或删除。验证链接是否指向正确的文件和位置 我需要将原始图像复制到临时文件夹中,然后将它们重新插入我的电子邮件中 我的宏可以将图像复制到临时文件夹中。如何将这些图像放入最终电子邮件中 更新:Vba 复制并转发正文中包含图像的电子邮件,vba,outlook,Vba,Outlook,我试图复制电子邮件的正文,并在用户转发之前将其放入模板中 原始电子邮件正文中的图像变成空白框,其中包含红色X 错误消息: 无法显示链接的图像。文件可能已被移动、重命名或删除。验证链接是否指向正确的文件和位置 我需要将原始图像复制到临时文件夹中,然后将它们重新插入我的电子邮件中 我的宏可以将图像复制到临时文件夹中。如何将这些图像放入最终电子邮件中 更新: 我知道了如何将临时文件中的图像作为隐藏附件添加到电子邮件中。(我更新了下面的代码)。我认为问题在于HTML图像标记仍然在引用我的旧电子邮件中图像
我知道了如何将临时文件中的图像作为隐藏附件添加到电子邮件中。(我更新了下面的代码)。我认为问题在于HTML图像标记仍然在引用我的旧电子邮件中图像的位置(ex:src=“cid:image001)。jpg@01D09693.82092260" 删除“@01D09693.82092260”会使标记从当前附件中获取图像吗?我该怎么做
Sub-ForwardEmail()
将项目设置为Outlook.MailItem
作为Outlook.MailItem前进的Dim
Dim olAttach作为Outlook.Attachments
将strFileN变为字符串
Set Item=GetCurrentItem
OWARD的集合=Application.CreateItemFromTemplate(“Z:\Template.oft”)
strFileN=Dir(“K:\Temp\*.*”)
向前
.Subject=项目.Subject
.HTMLBody=Item.HTMLBody和oford.HTMLBody
当Len(strFileN)>0时执行
.Attachments.Add“K:\Temp\”和strflen,olByValue,0
strFileN=Dir
环
.展示
.BodyFormat=olFormatHTML
以
终止“K:\Temp\*.*”
设置项=无
向前的集合=无
端接头
函数GetCurrentItem()作为对象
将objApp设置为Outlook.Application
作为Outlook.Attachments的Dim OBJAAttachments
Dim objSelection作为Outlook.Selection
我想我会坚持多久
暗计数等于长
作为字符串的Dim strFile
将strFolderpath设置为字符串
设置objApp=应用程序
'出现错误时,请继续下一步
选择案例类型名称(objApp.ActiveWindow)
案例“探索者”
设置GetCurrentItem=objApp.ActiveExplorer.Selection.Item(1)
案件“检查员”
设置GetCurrentItem=objApp.ActiveInspector.CurrentItem
结束选择
strFolderpath=“K:\Temp\”
Set-ObjaAttachments=GetCurrentItem.Attachments
lngCount=objAttachments.Count
如果lngCount>0,则
'使用倒计时循环删除项目
“从一个集合中。否则,循环计数器将
“混乱,只有其他项目被删除。
对于i=lngCount到1步骤-1
'获取文件名。
strFile=objAttachments.Item(i).FileName
'与临时文件夹的路径合并。
strFile=strFolderpath&strFile
'将附件另存为文件。
objAttachments.Item(i).SaveAsFile strFile
接下来我
如果结束
设置objApp=Nothing
Set objAttachments=Nothing
Set objSelection=Nothing
端函数
附件类的方法允许将文件附加到邮件
您还需要使用attachment.PropertyAccessor在附件上设置PR_ATTACH_CONTENT_ID属性(DASL-)。请注意,附件类的PropertyAccessor属性是在Outlook 2007中添加的
你可能会发现这个链接很有用
查看完整的示例代码。我自己解决了强> 我求助于使用正则表达式来删除有问题的十六进制路径,以便将图像链接到当前附加的图像。这花了相当长的时间让我的正则表达式正常工作,但这里是最后的代码
Sub ForwardEmail()
Dim Item As Outlook.MailItem
Dim oForward As Outlook.MailItem
Dim olAttach As Outlook.Attachments
Dim strFileN As String
Dim sBadHex As String
Set Item = GetCurrentItem
Set oForward = Application.CreateItemFromTemplate("Z:\Template.oft")
sBadHex = GetBadHex(Item.HTMLBody)
sEmailHTML = Replace(Item.HTMLBody, sBadHex, "")
strFileN = Dir("K:\Temp\*.*")
With oForward
.Subject = Item.Subject
.HTMLBody = sEmailHTML & oForward.HTMLBody
Do While Len(strFileN) > 0
.Attachments.Add "K:\Temp\" & strFileN, olByValue, 0
strFileN = Dir
Loop
'.BodyFormat = olFormatHTML <-- I don't think you need this
.Display
End With
Kill "K:\Temp\*.*"
Set Item = Nothing
Set oForward = Nothing
End Sub
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Set objApp = Application
'On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select
strFolderpath = "K:\Temp\"
Set objAttachments = GetCurrentItem.Attachments
lngCount = objAttachments.Count
If lngCount > 0 Then
' Use a count down loop for removing items
' from a collection. Otherwise, the loop counter gets
' confused and only every other item is removed.
For i = lngCount To 1 Step -1
' Get the file name.
strFile = objAttachments.Item(i).FileName
' Combine with the path to the Temp folder.
strFile = strFolderpath & strFile
' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile strFile
Next i
End If
Set objApp = Nothing
Set objAttachments = Nothing
Set objSelection = Nothing
End Function
Function GetBadHex(sInput As String) As String
Dim rImgTag As RegExp
Set rImgTag = New RegExp
Dim mImgTag As Object
Dim rBadHex As RegExp
Set rBadHex = New RegExp
Dim mBadHex As Object
Dim sImgTag As String
Dim sBadHex As String
With rImgTag
.Pattern = "cid:image[0-9]{3}\.[a-z]{3}@[0-9A-Z]{8}\.[0-9A-Z]{8}"
End With
With rBadHex
.Pattern = "@[0-9A-Z]{8}\.[0-9A-Z]{8}"
End With
Set mImgTag = rImgTag.Execute(sInput)
If mImgTag.Count <> 0 Then
sImgTag = mImgTag.Item(0)
End If
Set mBadHex = rBadHex.Execute(sImgTag)
If mBadHex.Count <> 0 Then
sBadHex = mBadHex.Item(0)
End If
GetBadHex = sBadHex
Set rImgTag = Nothing
Set rBadHex = Nothing
End Function
Sub-ForwardEmail()
将项目设置为Outlook.MailItem
作为Outlook.MailItem前进的Dim
Dim olAttach作为Outlook.Attachments
将strFileN变为字符串
作为字符串的Dim sBadHex
Set Item=GetCurrentItem
OWARD的集合=Application.CreateItemFromTemplate(“Z:\Template.oft”)
sBadHex=GetBadHex(Item.HTMLBody)
sEmailHTML=Replace(Item.HTMLBody,sBadHex,“”)
strFileN=Dir(“K:\Temp\*.*”)
向前
.Subject=项目.Subject
.HTMLBody=sEmailHTML&oForward.HTMLBody
当Len(strFileN)>0时执行
.Attachments.Add“K:\Temp\”和strflen,olByValue,0
strFileN=Dir
环
'.BodyFormat=olFormatHTML 0然后
'使用倒计时循环删除项目
“从一个集合中。否则,循环计数器将
“混乱,只有其他项目被删除。
对于i=lngCount到1步骤-1
'获取文件名。
strFile=objAttachments.Item(i).FileName
'与临时文件夹的路径合并。
strFile=strFolderpath&strFile
'将附件另存为文件。
objAttachments.Item(i).SaveAsFile strFile
接下来我
如果结束
设置objApp=Nothing
Set objAttachments=Nothing
Set objSelection=Nothing
端函数
函数GetBadHex(sInput作为字符串)作为字符串
Dim rImgTag作为RegExp
设置rImgTag=newregexp
Dim mImgTag作为对象
Dim rBadHex作为RegExp
设置rBadHex=newregexp
作为对象的Dim mBadHex
Dim sImgTag As字符串
作为字符串的Dim sBadHex
与rImgTag
.Pattern=“cid:image[0-9]{3}\[a-z]{3}@[0-9A-z]{8}\[0-9A-z]{8}”
以
使用rBadHex
.Pattern=“@[0-9A-Z]{8}\[0-9A-Z]{8}”
以
设置mImgTag=rImgTag.Execute(sInput)
如果mImgTag.Count为0,则
sImgTag=mImgTag.Item(0)
如果结束
设置mBadHex=rBadHex.Execute(sImgTag)
如果mBadHex.Count为0,则
sBadHex=mBadHex.项目(0)
如果结束
GetBadHex=sBadHex
设置rImgTag=无
设置rBadHex=Nothing
端函数