以下是我用来检查和处理某些Outlook电子邮件和附件的VBScript(VBS(。该脚本通过其电子邮件地址和主题查找电子邮件。然后,它将附件保存在文件夹中,并将电子邮件移动到Outlook中的文件夹中。(大部分代码改编自 stackoverflow.com 帖子,但后来我忘记了是哪一个。
我的问题:有时必须在用户打开当天的 Outlook 之前运行此脚本;因此,没有任何 Outlook 文件夹已更新,并且脚本找不到自用户上次关闭 Outlook 以来发送给用户的电子邮件。
我的问题:如何更新 Outlook 收件箱,然后继续执行脚本的其余部分,确保收件箱(或所有 Outlook 文件夹(已完全更新?我不确定 VBS 是否会等待文件夹更新发生,但如果不会,我当然需要它。我不知道如何更新收件箱或等待它更新(如果等待适用(。
欢迎提供有关如何使脚本更好的其他提示。
我的 VBScript:
Const olFolderInbox = 6
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
Call SaveAndMoveAttachment("'subject 1'", objFolder)
Call SaveAndMoveAttachment("'subject 2'", objFolder)
Call SaveAndMoveAttachment("'subject 3'", objFolder)
Set objFSO = Nothing
Set objOutlook = Nothing
Set objNamespace = Nothing
WScript.Quit
Sub SaveAndMoveAttachment(sSubject, objFolder)
Set colItems = objFolder.Items
Set colFilteredItems = colItems.Restrict("[Subject] = " & sSubject)
If colFilteredItems.count = 0 then
Msgbox "An email with subject " & sSubject & " in it was not found in your Outlook Inbox"
WScript.Quit
end if
For Each objMessage In colFilteredItems
Set colAttachments = objMessage.Attachments
intCount = colAttachments.Count
If intCount <> 0 and objMessage.Sender.Address = "support@somesite.com" Then
For i = 1 To intCount
strFileName = "Z:somepath" & objMessage.Attachments.Item(i).FileName
objMessage.Attachments.Item(i).SaveAsFile strFileName
'move the message to somefolder folder
Set objFoldersomefolder = objNamespace.GetDefaultFolder(olFolderInbox).Folders("somefolder")
objMessage.Move objFoldersomefolder
Next
End If
Next
Set colFilteredItems = Nothing
Set colAttachments = Nothing
Set colItems = Nothing
End Sub
在上述 2 行之间添加登录步骤
WSCript.Sleep 2000
objNamespace.Logon
objNamespace.SendAndReceive(True)
在此行下方:
Set objNamespace = objOutlook.GetNamespace("MAPI")
添加这个:
WSCript.Sleep 2000
objNamespace.SendAndReceive(True)
我在这里找到了它:http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/Q_28215854.html