I've just built a small util for sorting and filing reports received as emails in our public folders.
The util was initially an Outlook macro.
With help (See Having an Outlook 2003 SP3 macro run every day
), I've now got this as a VBS file which I can run via Windows Scheduler.
But I need to have Outlook open.
With it closed, I get an error ...
SortRecharges.vbs(23, 5) Microsoft Office Outlook: The messaging interface has returned an unknown error. If the problem persists, restart Outlook.
My VBS code is ...
' Public folder Unique ID - determine using code
s_RechargeFolderID = "000000001A447390AA6611CD9BC800AA002FC45A030085FB38B65F840A438204B33E62CC2FC4000000169FB80000"
Set o_App = CreateObject("Outlook.Application")
Set o_RechargeFolder = o_App.GetNamespace("Mapi").GetFolderFromID(s_RechargeFolderID)
' Verify Recharge Folder.
If o_RechargeFolder.Name <> "Recharge Reports" Then
m_Failure = MsgBox("Folder is not the Recharge Reports folder." & Chr(13) & Chr(13) & "Selected folder is : " & o_RechargeFolder.Name, vbExclamation + vbOKOnly, "ERROR : Invalid folder selected")
' Iterate the current contracts
For Each o_ContractFolder In o_RechargeFolder.Folders
' We ignore "Retired"
If "Retired" <> o_ContractFolder.Name Then
' Determine if we need to look at Year\Month\Day or Year only.
If "ASDA" = o_ContractFolder.Name Or "Hartshorne" = o_ContractFolder.Name Or "Hill Hire" = o_ContractFolder.Name Then
b_YMDFolder = True
b_YMDFolder = False
' Debug.Print "Examining " & o_ContractFolder.Name
' Wscript.StdOut.WriteLine("Examining " & o_ContractFolder.Name)
' Iterate the items.
i_Items = o_ContractFolder.Items.Count
i_Item = 0
' NOTE : Use a reverse for loop rather than for each as the pointer is updated to the next item immediately after the move and then Next skips an item.
For i_Item = i_Items To 1 Step of - 1
Set o_Item = o_ContractFolder.Items(i_Item)
' Debug.Print "Processing item #" & i_Item & " of " & i_Items & " reports : " & o_Item.Subject
' Wscript.StdOut.WriteLine("Processing item #" & i_Item & " of " & i_Items & " reports : " & o_Item.Subject)
' Mark as read
If True = o_Item.UnRead Then
o_Item.UnRead = False
' File the reports generated today in yesterday's folder.
dt_Received = o_Item.ReceivedTime - 1
' Find the appropriate destination file
If b_YMDFolder = True Then
Set o_YearFolder = CheckFolder(o_ContractFolder, CStr(Year(dt_Received)))
Set o_MonthFolder = CheckFolder(o_YearFolder, PadDigits(Month(dt_Received), 2))
Set o_DayFolder = CheckFolder(o_MonthFolder, PadDigits(Day(dt_Received), 2))
Set o_DayFolder = CheckFolder(o_ContractFolder, CStr(Year(dt_Received)))
Function CheckFolder(o_Folder, s_SubFolder)
Set CheckFolder = Nothing
On Error Resume Next
Set CheckFolder = o_Folder.Folders.Add(s_SubFolder)
On Error GoTo 0
If Not CheckFolder Is Nothing Then
Set CheckFolder = o_Folder.Folders(s_SubFolder)
' As the VBS environment has no Format() function, this code will provide the same effect as Format(i_Value, "00")
Function PadDigits(i_NumberToPad, i_TotalDigits)
PadDigits = Right(String(i_TotalDigits, "0") & i_NumberToPad, i_TotalDigits)
What do I need to add to this to get Outlook to open and close and to automatically select the correct mail profile (I've done some reading, but got nowhere with this actually working - which goes to show I'm missing something obvious).