下面是一个使用API
发布here蛮力方法。
Sub test()
Dim olApp As Outlook.Application: Set olApp = Outlook.Application
Dim objNS As Outlook.NameSpace: Set objNS = olApp.GetNamespace("MAPI")
Dim olFol As Outlook.MAPIFolder: Set olFol = objNS.GetDefaultFolder(olFolderInbox)
Set olFol = olFol.Folders("Test Folder") 'change to suit
Dim msg As Outlook.MailItem, att As Outlook.Attachment
Set msg = olFol.Items(olFol.Items.Count) 'change to suit
Dim strfile As String, fmsg As Outlook.MailItem
For Each att In msg.Attachments
If att.Type = 5 Then 'check if it is of olEmbeddedItem Type
strfile = Environ("Temp") & "\" & att.FileName
att.SaveAsFile strfile
'Use the function to open the file
ShellExecute 0, "open", strfile, vbNullString, vbNullString, 0
'Wait until it is open
Do While olApp.Inspectors.Count = 0: DoEvents
Loop
'Grab the inspector
Set fmsg = olApp.Inspectors.Item(1).CurrentItem
'Forward message
With fmsg.Forward
.To = "[email protected]"
.Send
End With
'Close and discard inspector
fmsg.Close 1: Set fmsg = Nothing '1 is for olDiscard
'Delete the file
Kill strfile
End If
Next
End Sub
这里是功能以防万一的链接断开
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
这是经得起考验的。所以首先,我试着测试文件夹中的最新消息在我的Inbox
中。
然后我们检查msg
是否有olEmbeddedItem
类型的附件(附件mailitem)。
请注意,您仍然需要检查msg
是否为MailItem
(我在测试中跳过了它)。
上面的两个答案是正确的,您需要保存该文件。
一旦保存,使用API
打开它,你需要的只是抓住Inspector
。
如果要用大量电子邮件重复此操作,则需要添加另一个循环。 HTH。
来源
2015-02-12 03:47:27
L42
三个答案之间我看到唯一正确的代码。我会离开我的投票。 – 2015-02-12 09:30:30
这真是太漂亮了!你救了我几个小时,可能几天。真诚的谢谢你。 一个后续行动,如果我可能:在循环中声明strFile和fmsg变量而不是在顶部是否有好处?我只在其他地方很少看到他们,但是这些看起来似乎是故意定位的。 – 2015-02-13 06:47:19
@MarkTangard很高兴帮助。顺便说一句,请参阅[接受答案](http://stackoverflow.com/help/someone-answers)作为SO中的一种说法。 – L42 2015-02-13 06:49:43