Results 1 to 14 of 14
  1. #1
    New Lounger
    Join Date
    Dec 2001
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    AttachmentAdd Event (Outlook 2002/VBA)

    I am trying to get the "objMailItem_AttachmentAdd" event to fire after adding an attachment to a new mail message. Can someone please tell me what I'm doing wrong?

    Here is my code which resides in the "ThisOutlookSession" module:

    Private WithEvents objMailItem As Outlook.MailItem

    Private Sub Application_Startup()
    Dim objMailItem As Outlook.MailItem
    End Sub

    Private Sub objMailItem_AttachmentAdd(ByVal Attachment As Attachment)
    MsgBox "test"
    End Sub

    When I add an attachment this event does not fire.

    Thanks
    Roland Thomas
    rpthomas@debevoise.com

  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

    Handling MailItem Events (Outlook 2002/VBA)

    Visual Basic for Applications is very specific about where you have to put certain variable declarations and code in order to intercept events in Office applications. I posted an example for Word, but in reviewing it I remembered a special frustration of working in Outlook: it's difficult to "get" the MailItem. My solution to this (presumably extracted from a long forgotten source) is to have Outlook watch for the creation of a new Inspector (message composition) window, as a way to get to the message itself. This should work for your application.

    I'm attaching three modules in a zip archive. The text is short enough to reproduce here, step by step:

    [list=1]<LI>Insert a new class module into your VbaProject. I named mine clsLounge. In your class module, add the following:

  3. #3
    Star Lounger
    Join Date
    Feb 2003
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AttachmentAdd Event (Outlook 2002/VBA)

    Original - close, but nothing is quite connecting. The trick here is to instantiate the mailitem object when it has something to handle... Let's use the NewInspector event to catch the creation of new mail items...
    Response - you are right, but this is much more complex than he needs for now - believe it or not, this can be done much more simply in Outlook2002: try this:

    Put this stuff in your ThisSession code space, and manually fire the InitializeHandler. Then switch to Outlook, create a new item, and insert an attachment - de msgbox comes up.

    Public WithEvents goInspectors As Outlook.Inspectors
    Public WithEvents mlItem As Outlook.MailItem

    Public Sub InitializeHandler()
    Set goInspectors = Outlook.Application.Inspectors
    End Sub

    Private Sub goInspectors_NewInspector(ByVal Inspector As Inspector)
    'let non-mail items through
    If Inspector.CurrentItem.Class = olMail Then
    Set mlItem = Inspector.CurrentItem
    Else
    Exit Sub
    End If
    End Sub

    Private Sub mlItem_AttachmentAdd(ByVal Attachment As Attachment)
    MsgBox "Heya"
    End Sub

  4. #4
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AttachmentAdd Event (Outlook 2002/VBA)

    Steve,

    When I create the new mail item, the CurrenItem is the empty string "".
    This gets assigned to mlItem and nothing happens.

    What am I missing?
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  5. #5
    Star Lounger
    Join Date
    Feb 2003
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AttachmentAdd Event (Outlook 2002/VBA)

    I hope your problem is as simple as me saying "CurrenItem"? don't you mean CurrentItem - with a "t" - Check for typos and such: this code really works on my machine (WXP/OXP)

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AttachmentAdd Event (Outlook 2002/VBA)

    There's something buggy about the whole thing. Not your code, per se.

    I've got OL 2002, BTW. I put the Inspectors instantiate code in the
    Application_Startup event, and coded up an mlItem_Open event.

    It doesn't work consistently. If I manually run InitializeHandler, it works.

    But starting Outlook and clicking New, does not fire my mlItemOpen event nor
    your Attachment_Add event (if I try to attach a file).

    In fact, the NewInspector event does not fire.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  7. #7
    Star Lounger
    Join Date
    Feb 2003
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AttachmentAdd Event (Outlook 2002/VBA)

    Anyone got experience with thei Application_Startup event that Kevin is trying? I always use Addins and an Addin designer, so I use the AddinInstance_OnConnection etc events for my initialization code - I've never tried Application_Startup.
    (BTW Kevin, if you go for the addin in approach, I have 100% reliabiility on this stuff.)

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AttachmentAdd Event (Outlook 2002/VBA)

    Steve,

    I'm with you completely on the addin approach. I'm just testing OL dev
    now and not really building anything. If I were it would be an Addin.

    I have a sneaky feeling that this issue relates to Outlook E-Mail Security Upate!
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  9. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AttachmentAdd Event (Outlook 2002/VBA)

    Ok, I'm confused. You no longer use Application_Startup, but

    Private Sub Application_Startup()
    ' Initialize event handler classes; turns off by itself
    ' if editing in VB editor... how annoying
    Set myEvents.colInspectors = Outlook.Inspectors
    End Sub

    is in your code from previous post.

    ??
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  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: AttachmentAdd Event (Outlook 2002/VBA)

    Try it my way. <img src=/S/wink.gif border=0 alt=wink width=15 height=15>

    I no longer use Application_Startup to init my event handler, but when I did, the macro warning dialog came up immediately, so I know it does fire when I start Oulook 2000 (CW).

    Afterthought: If you have any hidden instances of Outlook running, that could explain not running Application_Startup when you (think you are) starting Outlook.

  11. #11
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AttachmentAdd Event (Outlook 2002/VBA)

    OHHH!, my bad. Class mod. Class mod.

    I just caught that. Sorry.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  12. #12
    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: AttachmentAdd Event (Outlook 2002/VBA)

    I wrote that for the original poster. In reality, it's commented out.

    {Away for a while...}

    Okay, I uncommented it, logged out of OL, waited 30 seconds, restarted, got the macro warning dialog, enabled, and the handler is up and running.

  13. #13
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AttachmentAdd Event (Outlook 2002/VBA)

    I'll give it a go.

    BTW, what are we looking at in the userpic? Looks like turtles.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  14. #14
    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: AttachmentAdd Event (Outlook 2002/VBA)

    Yes, it's the face of a turtle who tries to hide in plain view on the outer reef at Ulua Beach, Maui. Good thing they're a protected species, or they'd all have been eaten by now. <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>

Posting Permissions

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