Link to home
Start Free TrialLog in
Avatar of billwhaley68
billwhaley68

asked on

Outlook VBA doesn't run when machine locked?

I'm using the following code in the ThisOutlookSession object to save file attachments on email messages with a given Subject. It works GREAT if I am working on the machine or have the application minimized. If I get up to walk away, I of course lock my machine. When the machine is locked (Outlook still running in background) and the email comes into my Inbox, the code does not appear to process. Is there way way around this so I don't have to leave my machine unlocked all of the time for it to run?

Thanks
-------------------------------------------------------------
Private WithEvents objInboxItems As Items

Private Sub Application_Startup()
    Dim objNS As NameSpace
    Set objNS = Application.GetNamespace("MAPI")
    ' instantiate Items collections for folders we want to monitor
    Set objInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
    Set objNS = Nothing
End Sub

Private Sub Application_Quit()
    ' disassociate global objects declared WithEvents
    Set objInboxItems = Nothing
End Sub

Private Sub objInboxItems_ItemAdd(ByVal Item As Object)
    Dim olItems As Items, _
        olItem As Object, _
        olMailItem As MailItem, _
        olAttachmentItem As Attachment
    Set olItems = objInboxItems.Restrict("[Unread] = True")
    For Each olItem In olItems
        If olItem.Class = olMail Then
            Set olMailItem = olItem
            'Change eeTesting on the next line to the subject you want to key on
            If InStr(1, olMailItem.Subject, "eeTesting", vbTextCompare) > 0 Then
                If olMailItem.Attachments.Count > 0 Then
                    For Each olAttachmentItem In olMailItem.Attachments
                        'Change the path on the next line to the path you want to save your attachments in
                        olAttachmentItem.SaveAsFile "C:\Attachments\" & olAttachmentItem.FileName
                    Next
                End If
            End If
        End If
    Next
End Sub
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of billwhaley68
billwhaley68

ASKER

I went back and looked at the solution I found it in (https://www.experts-exchange.com/questions/21261221/Automate-save-attachments-in-Outlook-2003-based-on-subject.html) and it was indeed yours.  As I mentioned, it works great until I lock the PC that it is running on. I did a test this morning and left Outlook running and locked the machine. I unlocked it several minutes later knowing one of the target emails was on the way. The email came into my Inbox, but no file copy happened. Since I am running it in a corporate environment, I really hate to walk away without locking my machine. It also runs fine minimized as well so long as I am logged in.

Since you are the original author (I'll bump up the points as well to 350), what would be the easiest way to monitor for more than one subject and save it off as well?

Thanks for your help
Hello BlueDevilFan -
I was just checking to see if you had by chance been able to verify the same results on your machine?

Thanks
Sorry to be slow.  I tested and am seeing the same behavior.  I've searched every Outlook/Exchange development book I own (several of those) and every web resource I can think of, and cannot find any mention of VBA code being halted when the computer is locked.  So, I've posted a question of my own to one of the Microsoft MVPs asking if they know why and if there's anything to be done about it.  I'll post back when I know more.
I never got this one to work while the machine was locked... your code works well when logged in however. Thanks for your help in trying to figure out why it wouldn't run on a locked machine.
Thanks for being so patient.  I never received a response to my question.  I've bookmarked this question and if I get an answer or come upon a solution, then I'll post it here.
Hi Guys,

Did you ever get an answer to your question?

I recently wrote a VBA script which is triggered on receipt of an email, however the email is received whilst I am not at work and therefore my computer is locked. It seems I am having the same issue.

I did however test this out by getting someone to send me an email just after I locked my PC and it worked fine, which makes me think that a service or something stops running after a certain amount of inactivity?

Thanks,

Tom
Tom,

Might the computer have gone to sleep?