troubleshooting Question

How to run VBA in outlook?

Avatar of stmoritz
stmoritz asked on
ProgrammingOutlookVB Script
5 Comments1 Solution900 ViewsLast Modified:
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]
ASKER CERTIFIED SOLUTION
Ken Butters

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros