Error Handling Loop...Stuck Inside!

Soule

Registered User.
Local time
Today, 12:47
Joined
Jan 6, 2012
Messages
28
I'm not sure why this code is hanging up. When my user clicks a form button, I'm simply trying to check to see if Outlook is open, and if it isn't, display a message box reminding them to open Outlook. If it is already open, I (obviously) want the code to skip the error code and continue.

My environment is:
- Access 2007
- .accdb file
- Am in datasheet view when stepping through
- Early binding
- Libraries:
VB for Apps
MS Access 12.0 Obj. Lib.
MS OFC 12.0 Access db engine Obj. Lib.
MS VB for Apps Extensibility 5.3
MS VBScript Regular Expressions 5.5
MS Outlook 12.0 Obj. Lib.
MS ActiveX Data Objects 2.8 Lib.
OLE Automation

The code is hanging up on the "Exit Sub" line.

When Outlook is closed: Hangs up on the "Exit Sub" when it should be going to SECT1_ERR.

When Outlook is open: Does the same thing, just runs faster.

When I take out the "Exit Sub", the error IS caught and the MsgBox pops, but then I get stuck in the loop of Resume Next-->GetObject-->MsgBox.

If Outlook is already opened or opened by the user after a first message instance, I don't see why my code won't continue from the "GetObject" statement to "Exit Sub" and on to the next section (SECT2) of my code....? No versions I'm trying work.

Here it is, along with the section that comes after it:

Code:
Option Explicit
Option Compare Database
 
Public Sub A1S1_Form_Re_send_Welcome_E_Mail_Only_Button_Click()
 
On Error GoTo SECT1_ERR:
 
Dim OlApp As Object
 
Set OlApp = GetObject("", "Outlook.Application")
 
' Debug.Print "See If Outlook Is Closed", Err.Number, Err.Description
 
Exit Sub
 
SECT1_ERR:
MsgBox "MS Outlook not open." & vbCrLf & "Please open Outlook, then hit automation button again." & vbCrLf & "Error #: " & Err.Number & " - " & 
Err.Description, vbExclamation + vbOKOnly, "Open Outlook"
 
Resume Next
 
 
' Template is now retrieved to base e-mail on...
On Error GoTo SECT2_ERR
Dim objOutlookMsg As Outlook.MailItem
 
Set objOutlookMsg = OlApp.CreateItemFromTemplate("J:\Database Work\A1 Tracking DB & Related\A1 Form Button Automation Email 
Templates\Employee A1 Welcome Outlook Template.oft")
 
' Debug.Print "Open template object", Err.Number, Err.Description
 
SECT2_EXIT:
Exit Sub
 
SECT2_ERR:
MsgBox "Error opening Outlook template object." & vbCrLf & "Error #: " & Err.Number & " - " & Err.Description, vbExclamation + vbOKOnly, "Open Outlook Template"
 
Resume SECT2_EXIT

Note: When I add a "SECT1_EXIT" before the error handling and a "Resume SECT1_EXIT" after it, code hangs up in the same place - on "SECT1_EXIT", when Outlook is open or not.
Note: I previously tried the CreateObject method to create an instance of Outlook if it was closed, but to no avail (got error 438-object doesn't support this property/method).

Thank you for looking at this problem. It is appreciated. Any advice will further be!

Frank

This post is also here: http://bytes.com/topic/visual-basic/answers/932425-error-handling-loop-stuck-inside#post3703866
 
Set oOutlook = CreateObject("Outlook.Application") works fine for me, office 2007. It grabs outlook whether open or not. Try it.

Btw - what you are doing here is LATE binding
 
What you are also doing is using GOTO programming disguised as error handling.

You can test if outlook is open without needing to use GOTO code blocks, if it's not open you can create it yourself.

Error Handling ideally should be for things you can't anticipate or can but can't sort out in code. And really with the exception of Error handling in VBA you should try and avoid using GOTO like the plague.

I don't use outlook much in my coding so I'd defer to SpikePl's suggestion that just using createObject("outlook.application") works regardless of whether Outlook is running or not. In some respects that may be preferably because that should give you an instance of outlook that belongs to your code, you can manipulate to do what you want and get rid of it without needing the user to do anything.
 
Thanks, guys. I couldn't get a CreateObject statement to work for me with a form in this environment, so I chucked it for now. The users will just have to use old school word of mouth that their automated messages won't actually be sent until they open their copy of Outlook.

I realized I was being TOO conservative with my error handling, and decided to go with one direction label at the beginning of my Sub and the error handler at the end of it...necessitating only one Exit Sub statement before the handler and End Sub statement. It was overly complicated before.

I also know the diff now between early and late binding. I thought it was only library related, but see now that it is dim and method related as well. Thanks, spikepl!

Thank you all!

Frank
 
Last edited:

Users who are viewing this thread

Back
Top Bottom