在Outlook中收到新邮件后,如何通过第二个变量运行?

时间:2017-03-26 03:12:21

标签: vba outlook outlook-vba

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

我正在使用上面的代码并且它在NEW EMAIL触发器上工作非常好(谢谢你 - Gautam Mainkar(LINK)。但是,我试图传递一个布尔(True / False)变量以及项目事件触发器。

所以我试图在Application_Startup()中设置说... MyTrueFalse,因此它只设置为ONCE,并在新电子邮件触发Items_ItemAdd时传递。

我不想要另一个子例程,只需按照Application_Startup()中的设置传递MyTrueFalse布尔值。

我在Items_ItemAdd子上尝试了多种公共设置和多个变量,但没有任何效果。我希望有人可以帮助我。感谢

哦,是的:它存在于ThisOutlookSession

1 个答案:

答案 0 :(得分:0)

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
蒂姆·威廉姆斯是对的,纠正了代码(上图),效果很好!谢谢蒂姆。麦克