We help IT Professionals succeed at work.

Outlook VBA doesn't run when machine locked?

billwhaley68
billwhaley68 asked
on
809 Views
Last Modified: 2016-09-02
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
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I went back and looked at the solution I found it in (https://www.experts-exchange.com/Applications/MS_Office/Outlook/Q_21261221.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

Author

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

Thanks
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.

Author

Commented:
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.
CERTIFIED EXPERT
Top Expert 2010

Commented:
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
CERTIFIED EXPERT
Top Expert 2010

Commented:
Tom,

Might the computer have gone to sleep?

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.