title: Outlook VBA: Script not triggering on email receive when computer is locked
body: <p>I have a VB script running on my outlook session that watches every new email coming in and is triggered when a specific email address and subject are received. It then saves the attachment from that email and triggers an excel macro to run.</p>
  
  <p>When I test this (by sending the email myself from another email address) it works fine. This morning, my computer received the email (but the computer was "locked", windows 7), but the script wasn't triggered. I know the vba should be working in the background, even when the computer is "locked", so i'm confused why this script didn't trigger.</p>
  
  <p>Does my VBA script not constantly run in the background, despite whether the computer is locked or not?</p>
  
  <p>Here's the code:</p>
  
  <blockquote>
    <p>Trigger macro "go" when receiving email from example@example.com AND subject = Today</p>
  </blockquote>
  
  <pre><code>Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
      'This sub catches every new email (or collection of emails)
      Dim intMsgIDStart As Integer, intMsgIDEnd As Integer
      Dim strMailItemID As String
      Dim cont As Boolean
  
      intMsgIDStart = 1
      intMsgIDEnd = InStr(intMsgIDStart, EntryIDCollection, ",")
  
      cont = True
      Do
          If (intMsgIDEnd &gt; 0) Then
              strMailItemID = Strings.Mid(EntryIDCollection, intMsgIDStart, (intMsgIDEnd - intMsgIDStart))
          Else
              strMailItemID = EntryIDCollection
          End If
          cont = handleMessage(strMailItemID)
          intMsgIDStart = intMsgIDEnd + 1
          intMsgIDEnd = InStr(intMsgIDStart, EntryIDCollection, ",")
      Loop While intMsgIDEnd &lt;&gt; 0 And cont
  
  End Sub
  
  Public Function handleMessage(strMailItemID As String)
      ' This function takes an email ID and determines whether it's our pricing sheet email.
      ' If so, it'll save it to the folder with a common name, pricing.xls
      ' Finally, it will call on runExcelMacro()
      On Error Resume Next
      Dim mailItem As Outlook.mailItem
      Dim path As String
      Dim result As Boolean
      result = True
      Set mailItem = Application.Session.GetItemFromID(strMailItemID)
      If (mailItem.SenderEmailAddress = "example@example.com" And Strings.Mid(mailItem.Subject, 1, 5) = "Today") Then
  
          path = "C:\path\Pricing.xls"
          mailItem.Attachments.item(1).SaveAsFile path
          Call runExcelMacro("C:\path\Auto.xlsm", "go")
          result = False
      End If
      handleMessage = result
  End Function
  
  Public Function runExcelMacro(path As String, macroName As String)
      'This function starts a macro
      Dim xl As Object
      Set xl = CreateObject("Excel.Application")
      xl.Workbooks.Open (path)
      'xl.Visible = False
      xl.Run macroName
  
      xl.ActiveWorkbook.Close (True)
      xl.Quit
  
      Set xl = Nothing
  
  End Function
  </code></pre>
  
  <p>Maybe I need to use a different setting to have VBA always run in the background?</p>
  
  <p>Thanks, and apologies that my question isn't as well defined as I would like - I'm just not even sure what is wrong!</p>
  
  <p>Cheers,</p>
  
  <p>Zac</p>