Is it possible / does anyone have an example of a script that will display the count of a user's unread email ? Can it work without Outlook being open ?
The background is that I would like to display a message stating the count of the user's unread email in an intranet page. The intranet page is launched from a log-on script, but Outlook isn't. Therefore, is it possible to get the unread email count without having Outlook open ?
(Sorry, I have a limited knowledge of MAPI, CDO, etc.)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' START OF USER CONFIGURABLE VARIABLES '''''''''''''''''''''''''''''''''''''''''''''
strLogFile="x:\somepath\somefile.log"
' END OF USER CONFIGURABLE VARIABLES '''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
PrintLog "Starting script"
' Get command line arguments.
Dim obArgs
Dim cArgs
Set obArgs = WScript.Arguments
cArgs = obArgs.Count
Main
Sub Main()
Dim oSession
Dim oInfoStores
Dim oInfoStore
Dim oInfoStore2
Dim oRootFolder
Dim oFolders
Dim StorageUsed
Dim NumMessages
Dim strProfileInfo
Dim sMsg
' On Error Resume Next
If cArgs <> 2 Then
printlog( "Usage: cscript script.vbs SERVERNAME MAILBOXNAME")
printlog( " e.g. cscript script.vbs svr smitha")
Exit Sub
End If
'Create Session object.
Set oSession = CreateObject("MAPI.Session")
if Err.Number <> 0 Then
sMsg = "Error creating MAPI.Session."
sMsg = sMsg & "Make sure CDO 1.21 is installed. "
sMsg = sMsg & Err.Number & " " & Err.Description
printlog( sMsg)
Exit Sub
End If
'Log on.
oSession.Logon , , False, True, , True, strProfileInfo
if Err.Number <> 0 Then
sMsg = "Error logging on: "
sMsg = sMsg & Err.Number & " " & Err.Description
printlog( sMsg)
printlog( "Server: " & obArgs.Item(0))
printlog( "Mailbox: " & obArgs.Item(1))
Set oSession = Nothing
Exit Sub
End If
'Grab the information stores.
Set oInfoStores = oSession.InfoStores
if Err.Number <> 0 Then
sMsg = "Error retrieving InfoStores Collection: "
sMsg = sMsg & Err.Number & " " & Err.Description
printlog( sMsg)
printlog( "Server: " & obArgs.Item(0))
printlog( "Mailbox: " & obArgs.Item(1))
Set oInfoStores = Nothing
Set oSession = Nothing
Exit Sub
End If
'Loop through information stores to find the user's mailbox.
For Each oInfoStore In oInfoStores
If InStr(1, oInfoStore.Name, "Mailbox - ", 1) <> 0 Then
'&HE080003 = PR_MESSAGE_SIZE
StorageUsed = oInfoStore.Fields(&HE080003)
if Err.Number <> 0 Then
sMsg = "Error retrieving PR_MESSAGE_SIZE: "
sMsg = sMsg & Err.Number & " " & Err.Description
printlog( sMsg)
printlog( "Server: " & obArgs.Item(0))
printlog( "Mailbox: " & obArgs.Item(1))
Set oInfoStore = Nothing
Set oInfoStores = Nothing
Set oSession = Nothing
Exit Sub
End If
sMsg = "Error Retrieving PR_CONTENT_COUNT: "
sMsg = sMsg & Err.Number & " " & Err.Description
printlog( sMsg)
printlog( "Server: " & obArgs.Item(0))
printlog( "Mailbox: " & obArgs.Item(1))
Set oInfoStore = Nothing
Set oInfoStores = Nothing
Set oSession = Nothing
Exit Sub
End If
ProcessMailbox(oInfoStore)
sMsg = "Storage Used in " & oInfoStore.Name
sMsg = sMsg & " (bytes): " & StorageUsed
printlog( sMsg)
printlog( "Number of Messages: " & NumMessages)
End If
Next
' Log off.
oSession.Logoff
' Clean up memory.
Set oInfoStore = Nothing
Set oInfoStores = Nothing
Set oSession = Nothing
End Sub
For Each oF in oIS.RootFolder.Folders
'Process top-level folders
'Ignore non message folders?
'If Name in ("Journal, Contacts, etc") then continue.
If oF.Name = "Calendar" OR oF.Name = "Contacts" OR oF.Name = "Drafts" _
OR oF.Name = "Tasks" OR oF.Name = "Notes" OR oF.Name = "Outbox" _
OR oF.Name = "Journal" OR oF.Name = "Calendar Attachments" Then
printlog ("Skipping: " & oIS.Name & "/" & oF.Name)
Else
printlog( "Processing: " & oIS.Name & "/" & oF.Name)
ProcessFolder(oF)
End If
Next
End Sub
Sub ProcessFolder(StartFolder)
Dim objFolder
Dim objItem
Dim iCount
printlog( " " & StartFolder.Name)
' process all the subfolders of this folder
For Each objFolder In StartFolder.Folders
Call ProcessFolder(objFolder)
Next
' process all the items in this folder
'If StartFolder.Name = "Test" Then
iCount=0
For Each objItem In StartFolder.Messages
'printlog "Item: " & iCount
********* ADD UNREAD COUNT HERE:::::: If objItem.Unread = True Then unread_count=unread_count+1
Call ProcessItem(objItem)
iCount=iCount+1
Next
printlog "Items processed: " & iCount
'End If
Set objFolder = Nothing
End Sub
Sub ProcessItem(objItem)
On Error Resume Next
'Resolve all Recipient addresses in the item
objItem.Recipients.Resolve(False)
If Err.Number <> 0 Then
Printlog "Error on objItem.Recipients.Resolve # " & Err.Number & " " & Err.Description
End If
' Save item changes
objItem.Update
If Err.Number <> 0 Then
Printlog "Error on objItem.Update # " & Err.Number & " " & Err.Description
End If
On Error Goto 0
' Clear memory
Set objItem = Nothing
Exit Sub
End Sub
Sub printlog(strMessage)
'Open log file
'Append to log file
'
Dim oFSO, oFile
Dim strData
Dim strPattern, strReplace
Dim strD
Const ForAppending = 8
strD = FormatDateTime(Now(),0)
Set oFSO = CreateObject("Scripting.FileSystemObject")
If NOT oFSO.FileExists(strLogFile) Then
Set oFile=oFSO.CreateTextFile(strLogFile)
oFile.WriteLine strD & " - " & "Log file created."
oFile.Close
End If
You can use vbscript to use the outlook object model. Outlook doesn't need to be open, it will start outlook.exe in the background but the outlook application won't be visible to the user.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.