Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VBScript, how do I update Microsoft Outlook Inbox folder before accessing it in VBS?

Status
Not open for further replies.

xfirebg

Programmer
Dec 8, 2017
1
BG
The following is a VBScript (VBS) that I use the check for and process certain Outlook emails and attachments. The script finds the emails via their email address and subject. It then saves the attachment in a folder and moves the email to a folder within Outlook.

Code:
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
I need to update the inbox if client is closed.
Another person told me to:
Add logon step between above 2 lines

WSCript.Sleep 2000
objNamespace.Logon
objNamespace.SendAndReceive(True)

And i stuck to add this 3 lines in the code. Can someone help me for this? Thanks :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top