0
votes

This is a followup to a previous question I had asked. Thank you to the community for your help with that.

I'm trying to create WithEvents code for the first time to check a folder for new items. Eventual plan is to use the ItemsAdd event to trigger a bunch of other processing, but for now, just trying to save it to a folder and not getting that far.

When I run the Application_Startup code below, the immediate window shows that I've found the right clntFldrItms. Problem is, if I then drag an item into the folder in question, the ItemAdd macro doesn't fire. When I try to add a watch for clntFldrItms, the variable isn't set to anything. It looks like as soon as the Application_Startup sub finishes, the assignment stops.

All code is in the ThisOutlookSession object.

Could this be because I'm working with an SMTP email address (rather than Exchange, for example)?

Thanks again for your help.

EDIT Adding my response to Eugene's comment. I noticed that when I open the editor and step into the Application_Startup sub, clntFldrItms is properly assigned, even before I get to the Set clntFldrItms = clntFldr.Items line. As soon as I finish stepping through, it's gone again. I can't step into the ItemAdd sub, but when I step into other code clntFldrItms is Nothing.

FINAL EDIT Sorry, I realize I forgot to close this off. I wasn't able to solve the problem per se, but I realized it was due to my SMTP account. When I tried it at work with Exchange, it worked. It seems that the event doesn't fire unless I'm working in Exchange.

Option Explicit

Public WithEvents clntFldrItms As Outlook.Items

Private Sub Application_Startup()
    Dim clntFldr As MAPIFolder
    Set clntFldr = Application.Session.GetDefaultFolder(olFolderSentMail).Folders("Client Emails")
    Set clntFldrItms = clntFldr.Items
    Set clntFldr = Nothing
    Debug.Print clntFldrItms.item(1).Subject
End Sub

Private Sub clntFldrItms_ItemAdd(ByVal item As Object)
    Dim bChar As String
    bChar = "\/:*?™""® <>|.&@#_+`©~;-+=^$!,'" & Chr(34)
    Dim saveName As String
    If item.Class = olMail Then
        saveName = item.Subject
        For x = 1 To Len(bChar)
            saveName = Replace(saveName, Mid(bChar, x, 1), "-")
        Next x
        item.SaveAs "C:\Users\User\Google Drive\8 - VBA work\Preparation for Assisted Responder\Sent Messages Folder\" & _
        saveName & ".msg", olMSG
    End If
End Sub
1
Are VBA macros allowed to run in Outlook? Did you have a chance to check out the Trust center settings?Eugene Astafiev
Thanks Eugene, I've already run a bunch of macros in my outlooks session. No dice...PKB
I've taken the code to work and it seems to be working on Outlook 2010 and Microsoft Exchange so the problem is solved as far as I'm concerned. I don't know whether the issue is with Outlook 2016 or the SMTP email address, but I suspect the latter. Happy for ideas, but if nobody has any, thanks for trying. CheersPKB

1 Answers

1
votes

Try to set a breakpoint in the ItemAdd event handler and check out the clntFldrItms object there when the breakpoint is hit.

Be aware, the ItemAdd event is not fired when multiple items were added at the same time (more than 16 - this is a well-known issue in Outlook).

You may find the Getting Started with VBA in Outlook 2010 article hellpful.

EDIT The clntFldrItms is set because the Startup event handler is run when you start Outlook. So, the object is initialized at startup behind the scene.