AOB
Registered User.
- Local time
- Today, 00:28
- Joined
- Sep 26, 2012
- Messages
- 637
Hi there,
I have a public function which connects to a defined Outlook mailbox folder and returns the number of e-mails within that folder (or, a null string, if for whatever reason it can't connect to the folder)
It's been running fine for weeks now but just today it's started causing me problems (the function is called as part of a FormLoad event, to populate a textbox)
Below is a shortened version of the code :
The weird thing is, whenever I try to load the form, the VBA errors on the line in red :
But if I set the next statement to the line in green (i.e. right-click and 'Set Next Statement...'), and continue from there, it runs fine. It's as if the code is skipping down to the line in red and has to be manually manipulated back to the start to pick up the object variables.
I'm stumped - especially as this worked up until this afternoon without any issues - I've obviously inadvertently changed something in the background but I haven't touched this function (or in fact this module whatsoever) so I have no idea where to even start?
Any suggestions?
Thanks
AOB
I have a public function which connects to a defined Outlook mailbox folder and returns the number of e-mails within that folder (or, a null string, if for whatever reason it can't connect to the folder)
It's been running fine for weeks now but just today it's started causing me problems (the function is called as part of a FormLoad event, to populate a textbox)
Below is a shortened version of the code :
Code:
Option Compare Database
Option Explicit
' Outlook objects
Private appOutlook As Object ' Outlook Application
Private objNameSpace As Object ' Outlook NameSpace
Private objMailbox As Object ' Outlook MAPIFolder
Private objInbox As Object ' Outlook MAPIFolder
Public Function CheckInbox(blnRetainObjects As Boolean) As Variant
[INDENT]Dim frm As Form
Dim i As Long
[COLOR=seagreen]Set frm = Forms("frmNavigation")[/COLOR]
' Connect to MS Outlook
Set appOutlook = CreateObject("Outlook.Application")
Set objNameSpace = appOutlook.GetNamespace("MAPI")
' Connect to mailbox
Set objMailbox = ConnectToFolder(objNameSpace, GetMailbox)
If objMailbox Is Nothing Then
[INDENT]CheckInbox = ""
GoTo ExitSub
[/INDENT]End If
' Connect to inbox
Set objInbox = ConnectToFolder(objMailbox, GetInbox)
If objInbox Is Nothing Then
[INDENT]CheckInbox = ""
GoTo ExitSub
[/INDENT]End If
' Return number of outstanding e-mails to be processed
[COLOR=red]CheckInbox = objInbox.Items.Count[/COLOR]
[/INDENT]ExitSub:
[INDENT]frm.txtEMailsOutstanding.Value = CheckInbox
If Not blnRetainObjects Then
[INDENT]Set appOutlook = Nothing
Set objNameSpace = Nothing
Set objMailbox = Nothing
Set objInbox = Nothing
Set frm = Nothing
[/INDENT]End If
[/INDENT]End Function
The weird thing is, whenever I try to load the form, the VBA errors on the line in red :
Run-time error '91':
Object variable or With block variable not set
But if I set the next statement to the line in green (i.e. right-click and 'Set Next Statement...'), and continue from there, it runs fine. It's as if the code is skipping down to the line in red and has to be manually manipulated back to the start to pick up the object variables.
I'm stumped - especially as this worked up until this afternoon without any issues - I've obviously inadvertently changed something in the background but I haven't touched this function (or in fact this module whatsoever) so I have no idea where to even start?
Any suggestions?
Thanks
AOB