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(olF olderInbox ).Items
Set objNS = Nothing
End Sub
Private Sub Application_Quit()
' disassociate global objects declared WithEvents
Set objInboxItems = Nothing
End Sub
Private Sub objInboxItems_ItemAdd(ByVa l Item As Object)
Dim olItems As Items, _
olItem As Object, _
olMailItem As MailItem, _
olAttachmentItem As Attachment
Set olItems = objInboxItems.Restrict("[U nread] = 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.Cou nt > 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.SaveAsFil e "C:\Attachments\" & olAttachmentItem.FileName
Next
End If
End If
End If
Next
End Sub
Thanks
--------------------------
Private WithEvents objInboxItems As Items
Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.GetNamespace("
' instantiate Items collections for folders we want to monitor
Set objInboxItems = objNS.GetDefaultFolder(olF
Set objNS = Nothing
End Sub
Private Sub Application_Quit()
' disassociate global objects declared WithEvents
Set objInboxItems = Nothing
End Sub
Private Sub objInboxItems_ItemAdd(ByVa
Dim olItems As Items, _
olItem As Object, _
olMailItem As MailItem, _
olAttachmentItem As Attachment
Set olItems = objInboxItems.Restrict("[U
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.Cou
For Each olAttachmentItem In olMailItem.Attachments
'Change the path on the next line to the path you want to save your attachments in
olAttachmentItem.SaveAsFil
Next
End If
End If
End If
Next
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello BlueDevilFan -
I was just checking to see if you had by chance been able to verify the same results on your machine?
Thanks
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.
ASKER
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
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?
Might the computer have gone to sleep?
ASKER
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