Results 1 to 13 of 13
  1. #1
    Star Lounger
    Join Date
    Jul 2003
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts

    NewMail Code problem upgrading from 2002 to 2003 (ol 2003)

    I had some VBA code running under Outlook 2002 that fired a reply email when a new one recieved has "BE" in the Subject. It does not work under 2003 and it looks like things may be a little different. Hopfully someone can help me sort-out why my code doesn't even seem to fire when I get a new email. Are there settings in Outlook that need adjusting (I disabled all "When new Items arrive in my inbox" options which I seem to remember having to do under 2002)?

    Thanks, keith


    <code>
    Private Sub Application_NewMail()

    Dim strSender, strSubject, strBody, strBEloc As String
    Set ol = GetObject(, "OUTLOOK.APPLICATION")
    Set MAPI = ol.GetNamespace("MAPI")
    Set mystore = MAPI.GetDefaultFolder(olFolderInbox)
    Set myItem = mystore.Items.Item(1)

    If myItem.Subject = "BE" Then 'looks for special message in "subject line"
    strSender = myItem.SentOnBehalfOfName 'assign return address, subject, and message body
    strSubject = (Date & " - Applications Database Snapshot")
    strBody = "Double click the attachment 'BE.exe' to open " _
    & "the Connected Off-Line version of the Applications " _
    & "Database. You must have installed this Off-Line Database " _
    & "for this to work."
    If Dir$("S:AppsAppsDBRemoteBE.exe") = vbNullString Then 'Check for file existance
    MsgBox "S:AppsAppsDBRemoteBE.exe is not there - no response will be sent to " _
    & myItem.RecievedByName, , "NightZEN"
    Exit Sub
    End If
    strBEloc = "S:AppsAppsDBRemoteBE.zip" 'Assign file
    Set MyMail = myItem.Reply 'create email as a reply
    MyMail.Subject = strSubject 'add Subject
    MyMail.Body = strBody 'add Body
    Set myAttachments = MyMail.Attachments
    myAttachments.Add strBEloc, olByValue, 1, "Back-End Snapshot of Applications Database"
    MyMail.Send 'Send

    'MsgBox "Sent BE", , "AppsDB Access Notification" 'notify upon sendinng
    Else
    Beep
    Beep
    Beep
    'strSender = myItem.SentOnBehalfOfName 'assign return address, subject, and message body
    'strSubject = "I am out of the office untill 03-08-04"
    'strBody = "Your message: '" & myItem.Subject & "', will be reviewed upon my return. " _
    ' & "Best Regards, Keith"
    '
    'Set MyMail = myItem.Reply 'create email as a reply
    'MyMail.Subject = strSubject 'add Subject
    'MyMail.Body = strBody 'add Body
    'MyMail.Send
    'MsgBox "Hey! You've got new mail!", , ""
    End If

    End Sub
    </code>

  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: NewMail Code problem upgrading from 2002 to 2003 (ol 2003)

    First, I guess it goes without saying that Outlook 2003's macro security settings may have changed. Since I don't have OL03, you'll need to be certain that the VBA project is loading properly and is trusted to the extent necessary.

    Second, try adding this to the top of your procedure to confirm whether the event is handled:

    Debug.Print "New Mail ran! " & Time

    Third, I assume you are putting your code in ThisOutlookSession. If not, what is your code to "initialize" the event? I haven't used the NewMail event, but for my keystroke, menu and toolbar events I have a class module, and some code to initialize my event handler. I posted some sample event hander code for Outlook in <post#=231866>post 231866</post#> and <post#=362311>post 362311</post#>.

    Finally, just for housekeeping, in light of the limited use of ol and MAPI, you can consolidate these lines:
    <UL>Set ol = GetObject(, "OUTLOOK.APPLICATION")
    Set MAPI = ol.GetNamespace("MAPI")
    Set mystore = MAPI.GetDefaultFolder(olFolderInbox)[/list]to this:
    <UL>Set mystore = Outlook.GetNamespace("MAPI").GetDefaultFolder(olFo lderInbox)[/list]Hope this helps.

  3. #3
    Star Lounger
    Join Date
    Jul 2003
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: NewMail Code problem upgrading from 2002 to 2003 (ol 2003)

    Thanks for your help. I have found out that I am handling the event with myy code, but have also discovered that in Office Outlook 2003, the items in the Items collection object are not guaranteed to be in any particular order. This said, when I try to get the first Item in Items, I am not getting the New Email, but another one in my inbox. Any Idea on how to get the new email Item using a different approach? I tried GetFirst with the same results.

    Thanks, Keith

  4. #4
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: NewMail Code problem upgrading from 2002 to 2003 (ol 2003)

    Did you try the last item, as in:

    Set myItem = mystore.Items(mystore.Items.Count)
    -John ... I float in liquid gardens
    UTC -7ąDS

  5. #5
    Star Lounger
    Join Date
    Jul 2003
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: NewMail Code problem upgrading from 2002 to 2003 (ol 2003)

    Sorry, I am not understanding what you are trying to ask.

  6. #6
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: NewMail Code problem upgrading from 2002 to 2003 (ol 2003)

    I'm suggesting that instead of the first Item in the Folder Items collection:

    Set myItem = mystore.Items.Item(1)

    that you select the last item, which can be found from the count of items in the folder, like this:

    Set myItem = mystore.Items(mystore.Items.Count)
    -John ... I float in liquid gardens
    UTC -7ąDS

  7. #7
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: NewMail Code problem upgrading from 2002 to 2003 (ol 2003)

    Keith, I found this discussion in OutlookCode which may help you.
    -John ... I float in liquid gardens
    UTC -7ąDS

  8. #8
    Star Lounger
    Join Date
    Jul 2003
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: NewMail Code problem upgrading from 2002 to 2003 (ol 2003)

    Thanks John, I will give this a shot.

  9. #9
    Star Lounger
    Join Date
    Jul 2003
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: NewMail Code problem upgrading from 2002 to 2003 (ol 2003)

    John,
    Items(mystore.Items.Count) works great. Thanks!!

  10. #10
    Star Lounger
    Join Date
    Jul 2003
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: NewMail Code problem upgrading from 2002 to 2003 (ol 2003)

    OK this is weird: I had some problems with my Outlook installation that required that I blow away my Cocuments and Settingskfw personal settings folder (this is a shortcut to reinstalling ol03 which saves some file or registry settings that recall your previous personal settings, which I needed changed).

    Anyway, when I went back into outlook and put the code back in (using <font color=4682b4> Set myItem = mystore.Items(mystore.Items.Count) </font color=4682b4> , it no longer grabbed the most recent email, but the oldest email in my inbox. Why now when it worked beautifully before?! I changed this line to:<font color=4682b4> Set myItem = mystore.Items(1)</font color=4682b4>, and it works fine now, grabbing the most recent email (the new one) on the NewMail event. I would love to understand the inconsistency here - anybody?


    <img src=/S/confused.gif border=0 alt=confused width=15 height=20>

  11. #11
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: NewMail Code problem upgrading from 2002 to 2003 (ol 2003)

    I don't have any insight, Keith. I personally recommend that for more robust code you either use the MAPIFolder.Items.ItemAdd technique in the OutlookCode link above, or you loop through all unread inbox itmes every time that the NewMail Event fires, like this code snippet:

    Private Sub Application_NewMail()
    Dim i As Integer
    With Outlook.GetNamespace("MAPI").GetDefaultFolder(olFo lderInbox)
    For i = 1 To .Items.Count
    With .Items(i)
    If .UnRead And InStr(.Subject, "BE") Then
    'your code here
    ' .UnRead = False ' if applicable
    ' .Save ' if applicable
    End If
    End With
    Next i
    End With
    End Sub
    -John ... I float in liquid gardens
    UTC -7ąDS

  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: NewMail Code problem upgrading from 2002 to 2003 (ol 2003)

    The order of items in a Folder's Items collection seems usually to be "order added to folder." Did you import your messages from another message store in newest-to-oldest order?

  13. #13
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: NewMail Code problem upgrading from 2002 to 2003 (ol 2003)

    Yeah, that's what I've always thought. It annoys me that when I run an Advanced Search, it finds the oldest items first. Slow, slow, slow. An option MS should have provided, like ascending and descending sorts.
    -John ... I float in liquid gardens
    UTC -7ąDS

Posting Permissions

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