Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unread Email Count

Status
Not open for further replies.

NickLowe

Programmer
Nov 14, 2002
26
GB
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 ?

Many thanks.
 
when you say open? does that mean you cant have an outlook.exe thread in the background which the user cant see??
 
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.)
 
i know it could be done using outlook.exe in the background, cant help you at the moment with how to achieve it with that though, sorry good luck
 
You can run this on a server with CDO installed (eg exchange server.)

You may wish to modify to only process the Inbox to generate the Inbox count...





'This script ..........

' USAGE: cscript ProcessNotesEmails.vbs SERVERNAME MAILBOXNAME

' This requires that CDO 1.21 is installed on the computer.



Dim rc, strDate, strLogFile

'Get date/time formatted as YYYYMMDD_HHMMSS
strDate= Year(Now) & Right(Month(Now)+100, 2) & Right(Day(Now)+100, 2) & "_" & Right(Hour(Now)+100, 2) & Right(Minute(Now)+100, 2) & Right(Second(Now)+100, 2)

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 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

strProfileInfo = obArgs.Item(0) & vbLf & obArgs.Item(1)

'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

'&H33020003 = PR_CONTENT_COUNT
NumMessages = oInfoStore.Fields(&H36020003)

if Err.Number <> 0 Then

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




Sub ProcessMailbox(oIS)
printlog( "S1: " & oIS.Name)
printlog( "S2: " & oIS.RootFolder.Name)

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

Set oFile = oFSO.OpenTextFile(strLogFile, ForAppending)
oFile.WriteLine strD & " - " & strMessage
oFile.Close

Wscript.Echo strD & " - " & strMessage

End Sub
 
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. has some excellent examples of using vbscript with outlook.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top