I was experimenting to write my own email detector - if Outlook is not open when you run this script then the first time it runs you get the message 'Operation Failed' - the second time it works. Works ok if Outlook is open. Does anyone know why? Thanks!
Dim objOL
Dim objNS
Dim objFolder
Dim objAllItems
Dim objItem
Dim nCount, nItems
Dim strCriteria
'nItems is the total items in Inbox
nItems = 0
'nCount is the unread ones
nCount = 0
Dim lAnswer
Set objOL = CreateObject("Outlook.Application"
Set objNS = objOL.GetNameSpace("MAPI"
Set objFolder = objNS.GetDefaultFolder(6)
Set objAllItems = objFolder.Items
nItems = objFolder.Items.Count
strCriteria = "[UnRead] = True"
Set objAllItems = objFolder.Items.Restrict(strCriteria)
For Each objItem In objAllItems
nCount=nCount+1
Next
lAnswer = MsgBox ("Mail(s) in your Inbox = " & nItems & vbCr & "Unread mails = "& nCount & vbCr & "Read your email now?", vbYesNo + vbQuestion + vbDefaultButton1, Date() & " "& Time())
If lAnswer = vbYes then
Set objOL=CreateObject("WScript.Shell"
objOL.Run "outlook.exe"
End if
Dim objOL
Dim objNS
Dim objFolder
Dim objAllItems
Dim objItem
Dim nCount, nItems
Dim strCriteria
'nItems is the total items in Inbox
nItems = 0
'nCount is the unread ones
nCount = 0
Dim lAnswer
Set objOL = CreateObject("Outlook.Application"
Set objNS = objOL.GetNameSpace("MAPI"
Set objFolder = objNS.GetDefaultFolder(6)
Set objAllItems = objFolder.Items
nItems = objFolder.Items.Count
strCriteria = "[UnRead] = True"
Set objAllItems = objFolder.Items.Restrict(strCriteria)
For Each objItem In objAllItems
nCount=nCount+1
Next
lAnswer = MsgBox ("Mail(s) in your Inbox = " & nItems & vbCr & "Unread mails = "& nCount & vbCr & "Read your email now?", vbYesNo + vbQuestion + vbDefaultButton1, Date() & " "& Time())
If lAnswer = vbYes then
Set objOL=CreateObject("WScript.Shell"
objOL.Run "outlook.exe"
End if