Link to home
Start Free TrialLog in
Avatar of Zac Harris
Zac HarrisFlag for United States of America

asked on

MsgBox in a Outlook Macro

This goes along with another question I asked on here and got the answer to. I would like to add a message box or prompt to signify that the Macro is complete. As it is now, it just sits there and the only way I know its doing something is if my inbox count goes down. I would like a box to pop up and say the process is complete. However, when I put the message box code at the end I get compile errors.

Here is my code:

Sub MoveAgedMail()

'adjust mailboxNameString to match your name - it will be the way it displays in outlook
    mailboxNameString = "email@test.com"

'Check to make sure variable stored string correctly.  Opportunity to kill script before it starts.
    MsgBox mailboxNameString
    
'Define outlook objects:
    Dim objOutlook As Outlook.Application
    Dim objNameSpace As Outlook.NameSpace
    Dim objSourceFolder As Outlook.MAPIFolder
    Dim obDestFolder As Outlook.MAPIFolder
    Dim objVariant As Variant

'Define Counters

    'Used for loop to comb through Inbox folder
    Dim intCount As Integer

    'Different ways to do with logic I did it quick and messy without it will make a copy in original folder and then also another folder that you set for all older mail if you setup similarly.
    Dim moveOnce As Integer
             
    ' Set variable values
    Set objOutlook = Application
    Set objNameSpace = objOutlook.GetNamespace("MAPI")
    Set objSourceFolder = objNameSpace.GetDefaultFolder(olFolderInbox)
             
    'Start loop to loop through each item in the inbox
    For intCount = objSourceFolder.Items.Count To 1 Step -1
    'This defines the variable as the mail item you are currently on in the loop which you can then manipulate, move or apply logic to.
        Set objVariant = objSourceFolder.Items.Item(intCount)

        DoEvents
        'Set move counter to 0 so it will reset upon every loop, it will add 1 if its processed by the logic and at the end if it had been passed by the custom folder rules it will move to the catch Older than a Day folder.
        'NOTE: Older than a day folder is completely optional you can leave out and items that dont match any othe folder / sender criteria will just remain in inbox.  Completely up to you.
        moveOnce = 0
        
        'Used for testing - Used to make sure that the variable was resetting.
        'MsgBox "Move Once = " & moveOnce
        
        'If object is a mail item Then...
        If objVariant.Class = olMail Then
            'Perform logic to determine difference between NOW and the date of the item.
            intDateDiff = DateDiff("d", objVariant.SentOn, Now)
            'If Date is older than 0 days (or today) then apply the following logic
            If intDateDiff > 0 Then
                'Define Sender of item
                strSender = objVariant.SenderName
               
                'IF Certain Sender then send to a folder
                'NOTE: Please check script description for a logical example to follow along with

'Move items older than a day to standalone folder to keep e-mails organized

'Example 1
                If strSender = "Voicemail Sender" Then
                    Set obDestFolder = objNameSpace.Folders(mailboxNameString).Folders("Inbox").Folders("Voicemail")
                    objVariant.Move obDestFolder
               'Add move counter so at the end item wont be moved to two locations
                    moveOnce = 1
                End If

            End If
        End If
    
    Next
    Exit Sub
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ken Butters
Ken Butters
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Apologize for the mistake
I was busy in my work simply copied from my code.

@Ken Butters
Thanks for pointing my mistake.
I guess below code is correct.



Sub MoveAgedMail()

'adjust mailboxNameString to match your name - it will be the way it displays in outlook
    mailboxNameString = "email@test.com"

'Check to make sure variable stored string correctly.  Opportunity to kill script before it starts.
    MsgBox mailboxNameString
   
'Define outlook objects:
    Dim objOutlook As Outlook.Application
    Dim objNameSpace As Outlook.NameSpace
    Dim objSourceFolder As Outlook.MAPIFolder
    Dim obDestFolder As Outlook.MAPIFolder
    Dim objVariant As Variant


On Error GoTo ErrorHandler


'Define Counters

    'Used for loop to comb through Inbox folder
    Dim intCount As Integer

    'Different ways to do with logic I did it quick and messy without it will make a copy in original folder and then also another folder that you set for all older mail if you setup similarly.
    Dim moveOnce As Integer
             
    ' Set variable values
    Set objOutlook = Application
    Set objNameSpace = objOutlook.GetNamespace("MAPI")
    Set objSourceFolder = objNameSpace.GetDefaultFolder(olFolderInbox)
             
    'Start loop to loop through each item in the inbox
    For intCount = objSourceFolder.Items.Count To 1 Step -1
    'This defines the variable as the mail item you are currently on in the loop which you can then manipulate, move or apply logic to.
        Set objVariant = objSourceFolder.Items.Item(intCount)

        DoEvents
        'Set move counter to 0 so it will reset upon every loop, it will add 1 if its processed by the logic and at the end if it had been passed by the custom folder rules it will move to the catch Older than a Day folder.
        'NOTE: Older than a day folder is completely optional you can leave out and items that dont match any othe folder / sender criteria will just remain in inbox.  Completely up to you.
        moveOnce = 0
       
        'Used for testing - Used to make sure that the variable was resetting.
        'MsgBox "Move Once = " & moveOnce
       
        'If object is a mail item Then...
        If objVariant.Class = olMail Then
            'Perform logic to determine difference between NOW and the date of the item.
            intDateDiff = DateDiff("d", objVariant.SentOn, Now)
            'If Date is older than 0 days (or today) then apply the following logic
            If intDateDiff > 0 Then
                'Define Sender of item
                strSender = objVariant.SenderName
               
                'IF Certain Sender then send to a folder
                'NOTE: Please check script description for a logical example to follow along with

'Move items older than a day to standalone folder to keep e-mails organized

'Example 1
                If strSender = "Voicemail Sender" Then
                    Set obDestFolder = objNameSpace.Folders(mailboxNameString).Folders("Inbox").Folders("Voicemail")
                    objVariant.Move obDestFolder
               'Add move counter so at the end item wont be moved to two locations
                    moveOnce = 1
                End If

            End If
        End If
   
    Next
MsgBox "Process completed successfully"
' you can customize the message box
    Exit Sub
ErrorHandler:
MsgBox "An error occurred "
'or use the below to see the error dont use both
MsgBox Err & ": " & Error(Err)
End Sub
@MAS --- yes much better.  Looks like that error routine would work now.

Just one other tip... when you post that much code, it is a good idea to surround it with code tags like the original poster did.  it is much easier to read that way.
Honestly I dont know how to embed the code
You highlight the code, and the click on the code button you see in the menu bar of experts-exchange "Post a Comment" window... it is similar to how you would accent with bold/italics/underline etc.

It will look similar to this after you do this...   <code>Some code</code>
The only difference is that instead of angle brackets <code> the word code will be surrounded with square brackets [].  I could not use square brackets to show you otherwise all you would see is embedded code.

You can either use the button to insert the tags, or you can manually type them in yourself.  Very similar to html or xml markup.
Avatar of Zac Harris

ASKER

@MAS

When you want to enter code just click the "Code" button on the toolbar of the "Post a Comment" section. Another way is to highlight your code then click the same "Code" button.
Ken Butters,

Thanks for your input on that. I was wondering where the label was for the ErrorHandling section of the code. I added it in place of the Exporttoexcel. I wouldn't want to export anything anyways as this macro is for my machine and I'd rather see the error on screen.

Thanks to all who helped!
Thanks to both and apologize for the mistake.
Thanks for the point and glad to know it helped.