Results 1 to 10 of 10
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    ReplyToAll event (Outlook 2003)

    There's a very useful bit of code on the MS web site for trapping the ReplyToAll event. The point is to avoid replying to all when only the main recipient is inteded to get the message. The instructions on the web site point out that you must first run the Initialize_Handler sub. once that's been run, the ReplyToAll event will kick in on the current message.

    We can't ask users to run the Initialize_Handler macro before replying to a message. How can I get this Initialize_Handler code to run whenver a message is opened? I've the code below, and it seems to run when I expect it to, but the ReplyToAll event does not kick in on the message.

    ** From MS web site:
    Public WithEvents myItem As MailItem

    Sub Initialize_Handler()
    Set myItem = Application.ActiveInspector.CurrentItem
    End Sub

    Private Sub myItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)
    Dim mymsg As String
    Dim myResult As Integer
    mymsg = "Do you really want to reply to all original recipients?"
    myResult = MsgBox(mymsg, vbYesNo, "Flame Protector")
    If myResult = vbNo Then
    Cancel = True
    End If
    End Sub

    ** My code to run Initialize_Handler
    If TypeOf Inspector.CurrentItem Is Outlook.MailItem Then
    Set msg = Inspector.CurrentItem ' check if new item
    If msg.Size = 0 Then ' 0 indicates it's a new message
    ' do nothing here
    Else ' size is > 0, therefore an incoming message
    Initialize_Handler
    End If
    Set msg = Nothing
    End If

    Thanks,
    Richard Barrett

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: ReplyToAll event (Outlook 2003)

    Must run, so no time to elaborate:

    In VB Editor, type Outlook.Inspectors, select Inspectors, press F1, under Events: NewInspector.

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: ReplyToAll event (Outlook 2003)

    Thanks. My code does use NewInspector; I just grabbed a snippet of code that was within the Sub.

    Maybe you can tell me something about my code, below. I Initialize the handler for ReplyToAll when I find a new inspector item that is a mail item and has a size > 0. This tells me the mail item is an inbound message... one that can be replied to. Initialize_Handler executes as expected. Then if I click ReplyToAll from the toolbar on the open message, I get the warning message. So far so good.

    However, this functions as expected only on one message per Outlook session... the one I first test it on. OlkItem "sticks" to that message. If I work with other messages in the Inbox, the ReplyToAll warning does not display. But if I go back to the message where it did work, it still works on that message. Am I missing something completely?

    Thanks for your help.
    Richard

    Option Explicit
    Dim WithEvents colInsp As Outlook.Inspectors
    Public WithEvents olkItem As MailItem

    Private Sub Application_Startup()
    Set colInsp = Application.Inspectors
    End Sub

    Sub Initialize_Handler()
    Set olkItem = Application.ActiveInspector.CurrentItem
    End Sub

    Private Sub colInsp_NewInspector(ByVal Inspector As Inspector)
    On Error GoTo DisableToFalse
    Dim SecurityManager As New AddInExpress.OutlookSecurityManager
    SecurityManager.DisableCDOWarnings = True
    SecurityManager.DisableOOMWarnings = True
    SecurityManager.DisableSMAPIWarnings = True
    Dim strMsg As String
    Dim msg As Outlook.MailItem
    Dim strTest As String
    Dim myResult As Integer
    Dim count As Integer
    Dim i As Integer
    On Error Resume Next
    Dim myMsg As String

    If TypeOf Inspector.CurrentItem Is Outlook.MailItem Then

    Set msg = Inspector.CurrentItem
    ' check if new item
    If msg.Size = 0 Then ' 0 indicates new message
    ' can't be replied to

    Else ' size is > 0, therefore an incoming message
    ' one that can be replied to
    Initialize_Handler
    End If
    Set msg = Nothing

    End If

    DisableToFalse:
    SecurityManager.DisableCDOWarnings = False
    SecurityManager.DisableOOMWarnings = False
    SecurityManager.DisableSMAPIWarnings = False

    End Sub

    Private Sub olkItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)
    Dim sMsg As String
    Dim iResult As Integer
    sMsg = "Do you really want to reply to all original recipients?"
    iResult = MsgBox(sMsg, vbYesNo, "Reply to All Warning")
    If iResult = vbNo Then
    Cancel = True
    End If
    End Sub

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: ReplyToAll event (Outlook 2003)

    If you are testing by opening messages and replying to them, then perhaps the problem is in the code. You have an error handler that would deny you access to any error information, so you might consider adding a MsgBox there.

    If you are testing with the VBE open and working with code in between, it's possible that the error handler becomes "disconnected." I am away from the office, but I have added a little menu with macros that initialize and de-initialize my event handlers. Maybe you can do the same thing.

  5. #5
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: ReplyToAll event (Outlook 2003)

    Thank you. Without OnError ResumeNext, the error was 91 - Object variable or With Block not set. I recognized this error as it occursif I run Initialize_Handler on a message that is not open, i.e. in the Inbox Inspector view. I also noticed that it was intermittent and seemingly had something to do with the way I double-clicked. (Perhaps that was my imagination.) It looks like the Set command in Initialize_Handler routine isn't always successful, and that the opening of the Inspector window somehow interferes. .

    I added an error trap to the Initialize_Handler sub. If err=91, I send it to label Again: which is at the top of the sub. I know this is awful coding technique, but it works well. I understand that the NewInspector event runs after the object is created, but before the window appears. This all sounds like a timing issue.

    Can you suggest a better way to accomplish the same result without spaghetti code? Do you agree with my interpretation of what's occuring?

    Thanks,
    Richard

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: ReplyToAll event (Outlook 2003)

    I'm away from my VbaProject.OTM, but I think you might need to invoke the Display method (either for the inspector or the message) or toggle a visibility property before the new inspector becomes the ActiveInspector. Or pass the Inspector object to your Initialize_Handler routine to bypass any ambiguity.

  7. #7
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: ReplyToAll event (Outlook 2003)

    Thanks. It doesn't seem that this should be necessay, does it? The documentation says the event occurs "whenever a new inspector window is opened, ... as a result of user action...." User action is opening the window in this case. The event fires when the new inspector window is created, but before the window displays. It does display correct, it just seems that the firing of the handler is "off" on some occasions.

    Since the window hasn't yet appeared, is the in fact a way for me to get the object in order to pass it to the handler? It's my undersanding that the only way to get the message object from the Inspector view is through a os-called "selection wrapper" which sounds very much outside my league. I read something by an Eastern Europen hack (genius level) about this. I've never seen anything in any of the reference books I own.

    Richard

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: ReplyToAll event (Outlook 2003)

    Sorry I was unclear. Your _NewInspector procedure does this:

    <code>Initialize_Handler</code>

    which looks like this:

    <code>Sub Initialize_Handler()
    Set olkItem = Application.ActiveInspector.CurrentItem
    End Sub</code>

    I think there might be a problem with "ActiveInspector" at the time this is called. And in fact you do not need to rely on ActiveInspector.In _NewInspector you have two more precise object references you can pass, if you redefine your Initialize_Handler() procedure with either of these parameters:

    <code>Sub Initialize_Handler(targetInsp as Inspector)
    Set olkItem = targetInsp.CurrentItem
    End Sub</code>

    <code>Sub Initialize_Handler(targetMI as MailItem)
    Set olkItem = targetMI
    End Sub</code>

    Or rather than redefine the external procedure, can you change your call in _NewInspector to the following?

    <code>Set olkItem = msg</code>

  9. #9
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: ReplyToAll event (Outlook 2003)

    Thanks... but you've lost me. If I call Initialize_Handler with the parameter you've suggested, targetInsp... how do I call Initialize_Handler? what is it I pass as target Insp? Or am I missing the point enitrely?

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: ReplyToAll event (Outlook 2003)

    Maybe I'm not using the right terminology. I see three alternatives for your two procedures:

    1. <LI>In colInsp_NewInspector use the following in place of <code>Initialize_Handler</code>:

      <code>Set olkItem = msg</code>

      <LI>In colInsp_NewInspector use the following in place of <code>Initialize_Handler</code>:

      <code>Initialize_Handler Inspector</code>
      or
      <code>Call Initialize_Handler(Inspector)</code>

      and revise the called procedure to:

      <code>Sub Initialize_Handler(targetInsp as Inspector)
      Set olkItem = targetInsp.CurrentItem
      End Sub</code>

      <LI>In colInsp_NewInspector use the following in place of <code>Initialize_Handler</code>:

      <code>Initialize_Handler msg</code>
      or
      <code>Call Initialize_Handler(msg)</code>

      and revise the called procedure to:

      <code>Sub Initialize_Handler(targetMI as MailItem)
      Set olkItem = targetMI
      End Sub</code>
    Does that clarify?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •