none
how to run outlook macro from excel macro RRS feed

  • Question

  • Hello,

    Question

    1)I have this Vba code i want this code to be run excel macro.. the code running but dont performed any action like moving mail

     2) how to call vba outlook macro in excel macro ?

    plz guys i need help urgnt

    Public Sub mail()

     On Error Resume Next
     Dim myNameSpace As Outlook.NameSpace
     Dim myInbox As Outlook.Folder
     Dim myDestFolder As Outlook.Folder
     Dim myItems As Outlook.Items
     Dim myItem As Object
     Set myNameSpace = Application.GetNamespace("MAPI")
     Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
     Set myItems = myInbox.Items
     Set myDestFolder = myInbox.Folders("POD A")
     Set myItem = myItems.Find("[Subject] = 'Job openings in DCS Technologies'")
     While TypeName(myItem) <> "Nothing"
     myItem.Move myDestFolder
     Set myItem = myItems.FindNext
     Wend
    End Sub

    Sunday, April 3, 2016 7:09 AM

Answers

  • Public Sub mail()
      Const olFolderInbox = 6
      Dim myApplication As Object 'Outlook.Application
      Dim myNameSpace As Object 'Outlook.Namespace
      Dim myInbox As Object 'Outlook.Folder
      Dim myDestFolder As Object 'Outlook.Folder
      Dim myItems As Object 'Outlook.Items
      Dim myItem As Object
      On Error GoTo Errorhandler
      Set myApplication = CreateObject("Outlook.Application")
      Set myNameSpace = myApplication.GetNamespace("MAPI")
      Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
      Set myItems = myInbox.Items
      Set myDestFolder = myInbox.Folders("POD A")
      Set myItem = myItems.Find("[Subject] = 'Job openings in DCS Technologies'")
      While Not myItem Is Nothing
        myItem.Move myDestFolder
        Set myItem = myItems.FindNext
      Wend
      
      Exit Sub
    Errorhandler:
      If Err.Source = "" Then Err.Source = Application.Name
      Debug.Print "Source     : " & Err.Source
      Debug.Print "Error      : " & Err.Number
      Debug.Print "Description: " & Err.Description
      If MsgBox("Error " & Err.Number & ": " & vbNewLine & vbNewLine & _
          Err.Description & vbNewLine & vbNewLine & _
          "Enter debug mode?", vbOKCancel + vbDefaultButton2, Err.Source) = vbOK Then
        Stop 'Press F8 twice
        Resume
      End If
    End Sub

    Sunday, April 3, 2016 8:16 AM

All replies

  • Public Sub mail()
      Const olFolderInbox = 6
      Dim myApplication As Object 'Outlook.Application
      Dim myNameSpace As Object 'Outlook.Namespace
      Dim myInbox As Object 'Outlook.Folder
      Dim myDestFolder As Object 'Outlook.Folder
      Dim myItems As Object 'Outlook.Items
      Dim myItem As Object
      On Error GoTo Errorhandler
      Set myApplication = CreateObject("Outlook.Application")
      Set myNameSpace = myApplication.GetNamespace("MAPI")
      Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
      Set myItems = myInbox.Items
      Set myDestFolder = myInbox.Folders("POD A")
      Set myItem = myItems.Find("[Subject] = 'Job openings in DCS Technologies'")
      While Not myItem Is Nothing
        myItem.Move myDestFolder
        Set myItem = myItems.FindNext
      Wend
      
      Exit Sub
    Errorhandler:
      If Err.Source = "" Then Err.Source = Application.Name
      Debug.Print "Source     : " & Err.Source
      Debug.Print "Error      : " & Err.Number
      Debug.Print "Description: " & Err.Description
      If MsgBox("Error " & Err.Number & ": " & vbNewLine & vbNewLine & _
          Err.Description & vbNewLine & vbNewLine & _
          "Enter debug mode?", vbOKCancel + vbDefaultButton2, Err.Source) = vbOK Then
        Stop 'Press F8 twice
        Resume
      End If
    End Sub

    Sunday, April 3, 2016 8:16 AM
  • thnk you andreas killer thnkx lotz
    Sunday, April 3, 2016 10:02 AM
  • Hello Mirza,

    Looks like you just need to automate Outlook from Excel. Take a look at the How to automate Outlook from another program article which describes all the required steps in depth.

    Sunday, April 3, 2016 11:28 AM