Outlook VB help



  • I'm using the following VB macro to move selected emails from Outlook's inbox to another folder ("Vendors" in this case).  This macro is assigned to a button so all I have to do is click the button to move the selected messages:

     

    Sub Vendors()
    On Error Resume Next

    Dim objFolder As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder
    Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem

    Set objNS = Application.GetNamespace("MAPI")
    Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
    Set objFolder = objInbox.Folders("Vendors")

    'Assume this is a mail folder
    If objFolder Is Nothing Then
        MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation, "INVALID FOLDER"
    End If

    ' Check if the Active window is an opened email
    If TypeName(Application.ActiveWindow) = "Inspector" Then
        Set objItem = Application.ActiveInspector.CurrentItem
        If objItem.Class = olMail Then
            objItem.Move objFolder
        End If
    Else
        'Require that this procedure be called only when a message is selected
        If Application.ActiveExplorer.Selection.Count = 0 Then
            MsgBox "No item selected."
            Exit Sub
        End If
        For Each objItem In Application.ActiveExplorer.Selection
            If objFolder.DefaultItemType = olMailItem Then
                If objItem.Class = olMail Then
                    objItem.Move objFolder
                End If
            End If
        Next
    End If

    Set objItem = Nothing
    Set objFolder = Nothing
    Set objInbox = Nothing
    Set objNS = Nothing

    End Sub

    Trouble is the string "Vendors" is hardcoded in the macro, so every time I add a new Inbox subfolder, I have to create the corresponding macro for it.  What I'd like is to pass the name of the button used to invoke the macro to the objInbox.Folders() method.  That way I'd have just one macro and be able to assign it buttons named for Inbox subfolders.

    Any ideas?

    Thanks!

     



  • Cant you pass the subfolder name to the macro as a string parameter? Then each seperate button would need to call the macro with a different parameter.



  • @tmm7 said:

    I'm using the following VB macro to move selected emails from Outlook's inbox to another folder ("Vendors" in this case).  This macro is assigned to a button so all I have to do is click the button to move the selected messages:

    Sub vendButton_OnClick()
       Vendors(Me.Name)
    END SUB

    Sub Vendors (By Val dstFolder as String)
    On Error Resume Next

    Dim objFolder As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder
    Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem

    Set objNS = Application.GetNamespace("MAPI")
    Set objInbox = objNS.GetDefaultFolder(olFolderInbox)


    'Assume this is a mail folder
    If objFolder Is Nothing Then
        MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation, "INVALID FOLDER"
    End If
    If dstFolder = "" or IsEmpty(dstFolder) THEN
    ' Figure out what to do if you don't recognize the button or its corresponding
    ' folder doesn't exist
    End If
    Set objFolder = objInbox.Folders(dstFolder)


    ' Check if the Active window is an opened email
    If TypeName(Application.ActiveWindow) = "Inspector" Then
        Set objItem = Application.ActiveInspector.CurrentItem
        If objItem.Class = olMail Then
            objItem.Move objFolder
        End If
    Else
        'Require that this procedure be called only when a message is selected
        If Application.ActiveExplorer.Selection.Count = 0 Then
            MsgBox "No item selected."
            Exit Sub
        End If
        For Each objItem In Application.ActiveExplorer.Selection
            If objFolder.DefaultItemType = olMailItem Then
                If objItem.Class = olMail Then
                    objItem.Move objFolder
                End If
            End If
        Next
    End If

    Set objItem = Nothing
    Set objFolder = Nothing
    Set objInbox = Nothing
    Set objNS = Nothing

    End Sub

    Trouble is the string "Vendors" is hardcoded in the macro, so every time I add a new Inbox subfolder, I have to create the corresponding macro for it.  What I'd like is to pass the name of the button used to invoke the macro to the objInbox.Folders() method.  That way I'd have just one macro and be able to assign it buttons named for Inbox subfolders.

    Any ideas?

    Thanks!

     



  • On Error Resume Next
     
    I guess the "Report abuse" function of this forum should be used now. How the hell do you trust your email to an "resume next"? 

Log in to reply