How to run VBA in outlook?

stmoritz
stmoritz used Ask the Experts™
on
I am completely new at Outlook VBA. So while in Excel for example, I place code in a module of the workbook and can then run it by clicking the play button or stepping through with F8, this seems not to work in Outlook.

I have Outlook 2010, Windows 7 64bit. In the Outlook VBA Window (Alt+F11) I have "ThisOutlookSession" and "Module1" under Modules. Currently, the code below is placed in "ThisOutlookSession".

For the time being, until the code is finished and works, I just want to run it manually by clicking [img of play button] or stepping through with F8. So If someobdy could help me what I have to do exactly to make it work.

When the ultra long code is finished (it will take a long time and a few more rounds of expert exchange questions I assume), it should then be run automatically when new mail arrives. But this solution is not yet needed, I will keep it for a later question.



Sub ProcessIncomingMessages(ByVal Item As Object)
'Private Sub Items_ItemAdd(ByVal Item As Object)

Dim objNS As Outlook.NameSpace
Set objNS = GetNamespace("MAPI")

'set outlook folder to scan
Dim OtlkDestFldr As Outlook.MAPIFolder
Set OtlkDestFldr = objNS.Folders("s@p*l.com").Folders("Inbox")



'SET CONDITION VARIABLES

Dim FromEmailAdrs As String 'defining partial string of sender email address
FromEmailAdrs = "id@gmx.net"

Dim EmailSubj As String 'defining partial or full string of email subject
EmailSubj = "Testmail für Outlook VBA"

Dim NotEmailSubj As String 'defining partial string that should NOT be in subject
NotEmailSubj = "Powerade"

Dim BdyStrng As String 'defining partial string in mail body
BdyStrng = "regards"

Dim NmbrOfAttch As Double 'defining required # of attachments
Dim AttchCountOperator As String
NmbrOfAttch = 1
AttchCountOperator = ">="

Dim ActualNmbrOfAttch As Double 'defining/getting actual # of attachments
Dim AttchCounter As Double
ActualNmbrOfAttch = Msg.Attachments.Count

Dim AttchNmString1 As String 'defining partial string(s) of  attachment(s) filename as 2 AND conditions
Dim AttchNmString2 As String
AttchNmString1 = "master"
AttchNmString2 = ".xls"

Dim NotAttchNmString As String 'defining partial string(s) NOT to be contained in  attachment(s) filename
NotAttchNmString = "txt"



'SET VARIABLES

Dim MsgSubj As String

Dim Today As Date 'get/set today's date in YYYY-MM-DD format
Today = DateSerial(Year(Date), Month(Date), Day(Date))

Dim PrevMth As String
PrevMth = DateSerial(Year(Date)) & "-" & DateSerial(Month(Date) - 1)

Dim NewMsgAttchPath As String
NewMsgAttchPath = "G:\data\xls\"
Dim NewMsgAttchFileName As String
NewMsgAttchFileName = "digestion_rep" & PrevMth & ".pdf"


 
'CHECK CONDITION VARIABLES
 
If TypeOf Item Is Outlook.MailItem Then
  Dim Msg As Outlook.MailItem
  Set Msg = Item
 
      'If Msg.SenderEmailAddress = "id@gmx.net" Then MsgBox ("yeah!") Else Exit Sub
      If InStr(Msg.SenderEmailAddress, FromEmailAdrs) <= 0 Then Exit Sub 'check fi sender email address contains defined string
      If InStr(Msg.Subject, EmailSubj) <= 0 Then Exit Sub 'check if mail subject contains defined string
      If InStr(Msg.Subject, NotEmailSubj) > 0 Then Exit Sub 'check if mail subject does not contain defined string
      If InStr(Msg.Body, BdyStrng) <= 0 Then Exit Sub 'check if defined string is contained in message body
      
      'check if # of attachments meets requirements set
          Select Case AttchCountOperator
             Case Is = "="
                If Msg.Attachments.Count = NmbrOfAttch Then End If
             Case Is = "<"
                If Msg.Attachments.Count < NmbrOfAttch Then End If
             Case Is = ">"
                If Msg.Attachments.Count > NmbrOfAttch Then End If
             Case Is = "<="
                If Msg.Attachments.Count <= NmbrOfAttch Then End If
             Case Is = ">="
                If Msg.Attachments.Count >= NmbrOfAttch Then End If
             Case Is = "<>"
                If Msg.Attachments.Count <> NmbrOfAttch Then End If
             Case Else
                Exit Sub
          End Select
    
        'check if attachment name contains defined string
        For AttchCounter = 1 To ActualNmbrOfAttch
            If InStr(Msg.Attachments(AttchCounter).fileName, AttchNmString1) <= 0 Then Exit Sub
            If InStr(Msg.Attachments(AttchCounter).fileName, AttchNmString2) <= 0 Then Exit Sub
        Next
        
        'alternative approach to check if attachement name contains defined string
        For Each Msg.Attachments In Msg.Attachments
            If InStr(Msg.Attachments.fileName, AttchNmString1) <= 0 Then Exit Sub
            If InStr(Msg.Attachments.fileName, AttchNmString2) <= 0 Then Exit Sub
        Next Msg.Attachments
            
        'check if attachment name does NOT contain defined string
        For AttchCounter = 1 To ActualNmbrOfAttch
            If InStr(Msg.Attachments(AttchCounter).fileName, NotAttchNmString) > 0 Then Exit Sub
        Next
      
      End If
      
      
      
'ACTIONS
etc. ... [makes no sense to post all code yet as it has many errors and the vast part is irrelevant for my question]

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
It's a little complicated, but this should get you started:
http://www.howto-outlook.com/howto/macrobutton.htm
This should will work the same way in outlook as it does in excel.  The reason you are not able to start this macro directly in Outlook is that your routine takes a parameter.

You will need to call this routine with a parameter in order to execute it.

For example : I just used a collection simply to quickly create an object.  I don't know what object you want to pass, but obviously you will need to change it to the correct paramter type.

Once you do that ... you can start execution of the "Main" routine just like you are trying to do with either F8 function key or the green start button.

If you tried to start a macro in Excel that takes a parameter, you would run into the same issue.


Sub Main()

  Dim newItem As Object
  
  Set newItem = New Collection
  
  
  ProcessIncomingMessages newItem
End Sub

Sub ProcessIncomingMessages(ByVal Item As Object)
'Private Sub Items_ItemAdd(ByVal Item As Object)

Dim objNS As Outlook.NameSpace
Set objNS = GetNamespace("MAPI")

'set outlook folder to scan
Dim OtlkDestFldr As Outlook.MAPIFolder
Set OtlkDestFldr = objNS.Folders("s@p*l.com").Folders("Inbox")

Open in new window

Alternatively you can just remove the parameter of your existing routine and then you could start it as you are describing.

Sub ProcessIncomingMessages()
.
.
.
End Sub

Open in new window

I corrected all the syntax errors in the code you previously posted so that execution will start when option explicit is used:

Note: I removed paramter to allow testing... when you actually go to use it, you will want to restore the parameter.

Sub ProcessIncomingMessages()
'Private Sub Items_ItemAdd(ByVal Item As Object)
Dim Item As Object

Dim objNS As Outlook.NameSpace
Dim Msg As MailItem


Set objNS = GetNamespace("MAPI")

'set outlook folder to scan
Dim OtlkDestFldr As Outlook.MAPIFolder
Set OtlkDestFldr = objNS.Folders("s@p*l.com").Folders("Inbox")



'SET CONDITION VARIABLES

Dim FromEmailAdrs As String 'defining partial string of sender email address
FromEmailAdrs = "id@gmx.net"

Dim EmailSubj As String 'defining partial or full string of email subject
EmailSubj = "Testmail für Outlook VBA"

Dim NotEmailSubj As String 'defining partial string that should NOT be in subject
NotEmailSubj = "Powerade"

Dim BdyStrng As String 'defining partial string in mail body
BdyStrng = "regards"

Dim NmbrOfAttch As Double 'defining required # of attachments
Dim AttchCountOperator As String
NmbrOfAttch = 1
AttchCountOperator = ">="

Dim ActualNmbrOfAttch As Double 'defining/getting actual # of attachments
Dim AttchCounter As Double
ActualNmbrOfAttch = Msg.Attachments.Count

Dim AttchNmString1 As String 'defining partial string(s) of  attachment(s) filename as 2 AND conditions
Dim AttchNmString2 As String
AttchNmString1 = "master"
AttchNmString2 = ".xls"

Dim NotAttchNmString As String 'defining partial string(s) NOT to be contained in  attachment(s) filename
NotAttchNmString = "txt"



'SET VARIABLES

Dim MsgSubj As String

Dim Today As Date 'get/set today's date in YYYY-MM-DD format
Today = DateSerial(Year(Date), Month(Date), Day(Date))

Dim PrevMth As String
PrevMth = DateSerial(Year(Date), Month(Date), Day(Date)) & "-" & DateSerial(Year(Date), Month(Date) - 1, Day(Date))

Dim NewMsgAttchPath As String
NewMsgAttchPath = "G:\data\xls\"
Dim NewMsgAttchFileName As String
NewMsgAttchFileName = "digestion_rep" & PrevMth & ".pdf"


 
'CHECK CONDITION VARIABLES
 
If TypeOf Item Is Outlook.MailItem Then

  Set Msg = Item
 
      'If Msg.SenderEmailAddress = "id@gmx.net" Then MsgBox ("yeah!") Else Exit Sub
      If InStr(Msg.SenderEmailAddress, FromEmailAdrs) <= 0 Then Exit Sub 'check fi sender email address contains defined string
      If InStr(Msg.Subject, EmailSubj) <= 0 Then Exit Sub 'check if mail subject contains defined string
      If InStr(Msg.Subject, NotEmailSubj) > 0 Then Exit Sub 'check if mail subject does not contain defined string
      If InStr(Msg.Body, BdyStrng) <= 0 Then Exit Sub 'check if defined string is contained in message body
      
      'check if # of attachments meets requirements set
          Select Case AttchCountOperator
             Case Is = "="
                If Msg.Attachments.Count = NmbrOfAttch Then
                End If
             Case Is = "<"
                If Msg.Attachments.Count < NmbrOfAttch Then
                End If
             Case Is = ">"
                If Msg.Attachments.Count > NmbrOfAttch Then
                End If
             Case Is = "<="
                If Msg.Attachments.Count <= NmbrOfAttch Then
                End If
             Case Is = ">="
                If Msg.Attachments.Count >= NmbrOfAttch Then
                End If
             Case Is = "<>"
                If Msg.Attachments.Count <> NmbrOfAttch Then
                End If
             Case Else
                Exit Sub
          End Select
    
        'check if attachment name contains defined string
        For AttchCounter = 1 To ActualNmbrOfAttch
            If InStr(Msg.Attachments(AttchCounter).FileName, AttchNmString1) <= 0 Then Exit Sub
            If InStr(Msg.Attachments(AttchCounter).FileName, AttchNmString2) <= 0 Then Exit Sub
        Next
        
        'alternative approach to check if attachement name contains defined string
        Dim myAttachment As Attachment
        For Each myAttachment In Msg.Attachments
            If InStr(myAttachment.FileName, AttchNmString1) <= 0 Then Exit Sub
            If InStr(myAttachment.FileName, AttchNmString2) <= 0 Then Exit Sub
        Next myAttachment
            
        'check if attachment name does NOT contain defined string
        For AttchCounter = 1 To ActualNmbrOfAttch
            If InStr(Msg.Attachments(AttchCounter).FileName, NotAttchNmString) > 0 Then Exit Sub
        Next
      
      End If
      
      
      
'ACTIONS
End Sub

Open in new window

Author

Commented:
Perfect! Thanks a lot! Exactly what I needed and just had to copy paste it! Thx a million!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial