Outllook Macro Runtime Error. Could not find object.
Posted on 2006-11-24
I have created a macro for a pop-up alert to indicate when a new mail message has arrived in a shared public folder inbox. It works without an issue on one set (Outlook 2003 SP2). On another, with the same Outlook configuration, I receive the doomed 'runtime error' indicating it can't find an object. I debug and it
highlights Set flr = ns.Folders(szDir)
The code is thanks to this site and some of my tweaking for path. Ultimately I want to tweak this to look at three public inboxes and alert our users that a new message has arrived. Perhaps there is a more efficient way in the code to do this and any help offered would be great.
Obviously I need to fix the runtime error before I go any further.
Here's the code (again thanks to BluDevilFan):
Private WithEvents olkFolder As Outlook.Items
Private WithEvents olkFolderOrders As Outlook.Items
Private Sub Application_Quit()
Set olkFolder = Nothing
Set olkFolderOrders = Nothing
Private Sub Application_Startup()
Set olkFolder = OpenMAPIFolder("\Mailbox - Searches Mailbox\Inbox").Items
Set olkFolderOrders = OpenMAPIFolder("\Mailbox - Orders - Two Rivers Title\Inbox").Items
Private Sub olkFolder_ItemAdd(ByVal Item As Object)
MsgBox "A new item has arrived in the SEARCHES Inbox."
Private Sub olkFolderOrders_ItemAdd(ByVal Item As Object)
MsgBox "A new item has arrived in the ORDERS Inbox."
Dim app, ns, flr, szDir, i
Set flr = Nothing
Set app = CreateObject("Outlook.Application")
If Left(szPath, Len("\")) = "\" Then
szPath = Mid(szPath, Len("\") + 1)
Set flr = app.ActiveExplorer.CurrentFolder
While szPath <> ""
i = InStr(szPath, "\")
If i Then
szDir = Left(szPath, i - 1)
szPath = Mid(szPath, i + Len("\"))
szDir = szPath
szPath = ""
If IsNothing(flr) Then
Set ns = app.GetNamespace("MAPI")
Set flr = ns.Folders(szDir)
Set flr = flr.Folders(szDir)
Set OpenMAPIFolder = flr
If TypeName(obj) = "Nothing" Then
IsNothing = True
IsNothing = False
Can someone point me in the right direction? I've patched and updated the PC that gets the runtime error. I've tried this searching only ONE inbox as well and the same issue.