这可能会令人困惑,但它就是这样。
我目前必须打开 Outlook 搜索标题/日期“XXX 20130624”,然后保存驱动器上具有指定名称的“模板文件”。即outlook附件主题为“ABC 20130624”,附加的excel文件标记为“ZBA Extra”,然后我将附件在网络上保存为“ABC”
任何帮助,将不胜感激。
这是您需要执行的操作的分步说明。该代码是半伪代码,不能开箱即用。询问任何不清楚的细节。
1.0 将Outlook的运行实例获取到对象中
outlook = GetObject( , "Outlook.Application" )
2.0 然后访问收件箱(olFolderInbox = 6)文件夹。这必须通过命名空间对象来完成
namespace = outlook.GetNameSpace("MAPI")
inboxFolder = namespace.GetDefaultFolder(olFolderInbox)
3.0 在此文件夹中,搜索所需的文档。您的要求在那里特别模糊,我假设任何以硬编码日期结尾的主题都可以。我在这里使用 DASL 语法来执行like
运算符。
filter = ""@SQL=""http://schemas.microsoft.com/mapi/proptag/0x0037001f"" = ' 20130628'
items = inboxFolder.Items.Restrict( filter )
4.0 枚举遇到的项目。
For Each item in items
...
Next item
4.1。对于每个项目,存储主题,从第一个空格中删除任何内容
subject = mailItem.Subject
spaceIndex = InStr( subject, " " )
extractFilename = Left( subject, spaceIndex )
4.2 枚举附件并以名称保存。
For Each attachment In Item.Attachments
fileName = "H:\" & exdtractedFilename & ".xlsx"
attachment.SaveAsFile fileName
Next attachment
请注意,如果有多个附件,这将多次覆盖同一个文件。您可能想考虑如何处理这种情况。
Public Sub Extract_Outlook_Email_Attachments()
Dim OutlookOpened As Boolean
Dim outApp As Outlook.Application
Dim outNs As Outlook.Namespace
Dim outFolder As Outlook.MAPIFolder
Dim outAttachment As Outlook.Attachment
Dim outItem As Object
Dim saveFolder As String
Dim outMailItem As Outlook.MailItem
Dim inputDate As String, subjectFilter As String
saveFolder = "Y:\Wingman" ' THIS IS WHERE YOU WANT TO SAVE THE ATTACHMENT TO
If Right(saveFolder, 1) <> "\" Then saveFolder = saveFolder & "\"
subjectFilter = ("Daily Operations Custom All Req Statuses Report") ' THIS IS WHERE YOU PLACE THE EMAIL SUBJECT FOR THE CODE TO FIND
OutlookOpened = False
On Error Resume Next
Set outApp = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Set outApp = New Outlook.Application
OutlookOpened = True
End If
On Error GoTo 0
If outApp Is Nothing Then
MsgBox "Cannot start Outlook.", vbExclamation
Exit Sub
End If
Set outNs = outApp.GetNamespace("MAPI")
Set outFolder = outNs.GetDefaultFolder(olFolderInbox)
If Not outFolder Is Nothing Then
For Each outItem In outFolder.Items
If outItem.Class = Outlook.OlObjectClass.olMail Then
Set outMailItem = outItem
If InStr(1, outMailItem.Subject, "subjectFilter") > 0 Then
For Each outAttachment In outMailItem.Attachments
outAttachment.SaveAsFile saveFolder & outAttachment.filename
Set outAttachment = Nothing
Next
End If
End If
Next
End If
If OutlookOpened Then outApp.Quit
Set outApp = Nothing
End Sub