3
votes
Private WithEvents Items As Outlook.Items
Public MyTrueFalse As Boolean 
Private Sub Application_Startup()
  Dim MyTrueFalse As Boolean 'Redundant?'
  MyTrueFalse = True 'Defaults to False'  
  Dim olApp As Outlook.Application 
  Dim objNS As Outlook.NameSpace 
  Set olApp = Outlook.Application 
  Set objNS = olApp.GetNamespace("MAPI") 
  ' default local Inbox
  Set Items = objNS.GetDefaultFolder(olFolderInbox).Items 
End Sub
Private Sub Items_ItemAdd(ByVal item As Object, ByVal MyTrueFalse As Boolean) 
  If MyTrueFalse Then GoTo DoThisAlso 'Example Only'
  On Error Goto ErrorHandler 
  Dim Msg As Outlook.MailItem 
  If TypeName(item) = "MailItem" Then
    Set Msg = item 
    ' ******************
    ' do something here
    ' ******************
  End If
DoThisAlso:
   MsgBox "MyTrueFalse is: " & MyTrueFalse
ProgramExit: 
  Exit Sub
ErrorHandler: 
  MsgBox Err.Number & " - " & Err.Description 
  Resume ProgramExit 
End Sub

I am using the code above and its working wonderfully on the NEW EMAIL trigger (THANK YOU - Gautam Mainkar (LINK). However, I am trying to pass a Boolean (True/False) variable along with the Items event trigger.

So I am attempting to set say...MyTrueFalse within the Application_Startup() so it is set ONLY ONCE, and passed whenever Items_ItemAdd is triggered by a new email.

I do not want another sub routine, just pass MyTrueFalse boolean as set in the Application_Startup().

I have tried multiple variations of Public settings and multiple variables on the Items_ItemAdd sub and nothing works. I am hoping someone can help me here. Thanks

Oh Yeah: it resides in ThisOutlookSession

1
Are you getting error? is yes what is the error?0m3r
The errors vary depending on which attempt variation I am executing. Mostly Compile Errors. VBA doesn't seem to like the added variable on Items_ItemAdd sub line. MyTrueFalse is never TRUE always the defaulted FALSE.Automation Developer
You cannot add new parameters to the built-in event handlers. you already have your variable as a global, so you can access it without needing to pass it as an argument.Tim Williams
Hi Tim, so your saying remove ByVal MyTrueFalse As Boolean, and it will work as programmed...Yes? I can try that, but I thought I already did, but will attempt again. Thanks.Automation Developer

1 Answers

0
votes
Private WithEvents Items As Outlook.Items
Private MyTrueFalse As Boolean 
Private Sub Application_Startup()
  MyTrueFalse = True  
  Dim olApp As Outlook.Application 
  Dim objNS As Outlook.NameSpace 
  Set olApp = Outlook.Application 
  Set objNS = olApp.GetNamespace("MAPI") 
  ' default local Inbox
  Set Items = objNS.GetDefaultFolder(olFolderInbox).Items 
End Sub
Private Sub Items_ItemAdd(ByVal item As Object) 
  If MyTrueFalse Then GoTo DoThisAlso 'Example Only'
  On Error Goto ErrorHandler 
  Dim Msg As Outlook.MailItem 
  If TypeName(item) = "MailItem" Then
    Set Msg = item 
    ' ******************
    ' do something here
    ' ******************
  End If
DoThisAlso:
   MsgBox "MyTrueFalse is: " & MyTrueFalse
ProgramExit: 
  Exit Sub
ErrorHandler: 
  MsgBox Err.Number & " - " & Err.Description 
  Resume ProgramExit 
End Sub

Tim Williams was right, corrected the code (above) and works great!! Thanks Tim. Mike