Connecting Tech Pros Worldwide Forums | Help | Site Map

Am I doing this correctly?

LaurenW
Guest
 
Posts: n/a
#1: Nov 12 '05
After the "ExitHere" below, I am closing my object vars and setting
their values to nothing. Am I doing it correctly?

Thanks for all responses.

'================= BEGIN CODE ================================
Public Sub CreateOLMessage(msgTo As String, _
msgSubj As String, _
msgBdy As String, _
ViewAfter As Boolean, _
HTMLFmt As Boolean, _
Attach As String)

''This sub creates a new email message using output from the LMS
Message manager form.

Dim objOutlook As Object ' Late bind
Dim ns As Object ' Late bind
Dim objMsg As Object
Dim strAttachments As String
Dim strAttach() As String 'Dynamic array for analyzing
attachment availability

On Error GoTo HandleErr

If IsOutlook = 0 Then
ErrMsg NoOutlookMsg
Exit Sub
End If

Set objOutlook = CreateObject("Outlook.application")
Set ns = objOutlook.GetNamespace("MAPI")
ns.Logon

Set objMsg = objOutlook.CreateItem(0) 'Const olMailItem = 0
objMsg.to = msgTo
objMsg.Subject = msgSubj

If Len(msgBdy) > 0 Then
If HTMLFmt = False Then
objMsg.body = msgBdy
Else
objMsg.HTMLBody = msgBdy
End If
End If

'=== Are there any attachments? If so, append them:
' Add attachments to MailItem object's Attachments
' collection.

EmailSent = True

With objMsg
If Len(Attach) > 0 Then
If InStr(Attach, ";") = 0 Then
If IsFile(Attach) Then
.Attachments.Add Attach
Else
ErrMsg "Attachment file: " & Attach & " not found."
End If
Else
strAttachments = Attach
If AttachmentsOK(Attach) Then
Do
.Attachments.Add Left(strAttachments,
InStr(strAttachments, ";") - 1)
strAttachments = Trim(MID(strAttachments,
InStr(strAttachments, ";") + 1))
Loop While InStr(strAttachments, ";") <> 0
If Len(strAttachments) > 0 Then .Attachments.Add
strAttachments
Else
EmailSent = False
GoTo ExitHere
End If
End If
End If
End With

If ViewAfter = True Then
objMsg.Display
Else
objMsg.Send
End If

GoTo ExitHere

ExitHere:
objMsg.Close
Set objMsg = Nothing
objOutlook.Close
Set objOutlook = Nothing
ns.logoff
Set ns = Nothing

Exit Sub

' Error handling block added by Error Handler Add-In. DO NOT EDIT this
block of code.
' Automatic error handler last updated at 11-12-2002 17:34:33
'ErrorHandler:$$D=11-12-2002 'ErrorHandler:$$T=17:34:33
HandleErr:
Select Case Err.Number
Case 287
Resume Next
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbCritical, "basLMS.CreateOLMessage"
'ErrorHandler:$$N=basLMS.CreateOLMessage
End Select
' End Error handling block.
End Sub

'================= END CODE ==================================

Wayne Morgan
Guest
 
Posts: n/a
#2: Nov 12 '05

re: Am I doing this correctly?


You don't need to GoTo ExitHere right above the ExitHere routine because
that is where you'll go next anyway. In your error handler, if the error is
287 you Resume Next, but if the error is Case Else you give a message and
drop out the bottom of the sub. You should put in a Resume ExitHere before
the End Sub so that you go back up and clean up your variables. At the start
of your ExitHere routine you may want to use On Error Resume Next, that way
if you get to the ExitHere (due to an error that was run through the error
routine) be fore you Open something, such as objMessage, you won't receive
an error that it can't close the unopened item.

GoTo ExitHere 'Remove this

ExitHere:
On Error Resume Next
objMsg.Close
Set objMsg = Nothing
objOutlook.Close
Set objOutlook = Nothing
ns.logoff
Set ns = Nothing

Exit Sub

' Error handling block added by Error Handler Add-In. DO NOT EDIT this
block of code.
' Automatic error handler last updated at 11-12-2002 17:34:33
'ErrorHandler:$$D=11-12-2002 'ErrorHandler:$$T=17:34:33
HandleErr:
Select Case Err.Number
Case 287
Resume Next
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbCritical, "basLMS.CreateOLMessage"
'ErrorHandler:$$N=basLMS.CreateOLMessage
End Select
' End Error handling block.
Resume ExitHere
End Sub


--
Wayne Morgan
Microsoft Access MVP


<LaurenW> wrote in message
news:c4bm005mosullo4vhk2ebe37t3jvd2gu9h@4ax.com...[color=blue]
> After the "ExitHere" below, I am closing my object vars and setting
> their values to nothing. Am I doing it correctly?
>
> Thanks for all responses.
>
> '================= BEGIN CODE ================================
> Public Sub CreateOLMessage(msgTo As String, _
> msgSubj As String, _
> msgBdy As String, _
> ViewAfter As Boolean, _
> HTMLFmt As Boolean, _
> Attach As String)
>
> ''This sub creates a new email message using output from the LMS
> Message manager form.
>
> Dim objOutlook As Object ' Late bind
> Dim ns As Object ' Late bind
> Dim objMsg As Object
> Dim strAttachments As String
> Dim strAttach() As String 'Dynamic array for analyzing
> attachment availability
>
> On Error GoTo HandleErr
>
> If IsOutlook = 0 Then
> ErrMsg NoOutlookMsg
> Exit Sub
> End If
>
> Set objOutlook = CreateObject("Outlook.application")
> Set ns = objOutlook.GetNamespace("MAPI")
> ns.Logon
>
> Set objMsg = objOutlook.CreateItem(0) 'Const olMailItem = 0
> objMsg.to = msgTo
> objMsg.Subject = msgSubj
>
> If Len(msgBdy) > 0 Then
> If HTMLFmt = False Then
> objMsg.body = msgBdy
> Else
> objMsg.HTMLBody = msgBdy
> End If
> End If
>
> '=== Are there any attachments? If so, append them:
> ' Add attachments to MailItem object's Attachments
> ' collection.
>
> EmailSent = True
>
> With objMsg
> If Len(Attach) > 0 Then
> If InStr(Attach, ";") = 0 Then
> If IsFile(Attach) Then
> .Attachments.Add Attach
> Else
> ErrMsg "Attachment file: " & Attach & " not found."
> End If
> Else
> strAttachments = Attach
> If AttachmentsOK(Attach) Then
> Do
> .Attachments.Add Left(strAttachments,
> InStr(strAttachments, ";") - 1)
> strAttachments = Trim(MID(strAttachments,
> InStr(strAttachments, ";") + 1))
> Loop While InStr(strAttachments, ";") <> 0
> If Len(strAttachments) > 0 Then .Attachments.Add
> strAttachments
> Else
> EmailSent = False
> GoTo ExitHere
> End If
> End If
> End If
> End With
>
> If ViewAfter = True Then
> objMsg.Display
> Else
> objMsg.Send
> End If
>
> GoTo ExitHere
>
> ExitHere:
> objMsg.Close
> Set objMsg = Nothing
> objOutlook.Close
> Set objOutlook = Nothing
> ns.logoff
> Set ns = Nothing
>
> Exit Sub
>
> ' Error handling block added by Error Handler Add-In. DO NOT EDIT this
> block of code.
> ' Automatic error handler last updated at 11-12-2002 17:34:33
> 'ErrorHandler:$$D=11-12-2002 'ErrorHandler:$$T=17:34:33
> HandleErr:
> Select Case Err.Number
> Case 287
> Resume Next
> Case Else
> MsgBox "Error " & Err.Number & ": " & Err.Description,
> vbCritical, "basLMS.CreateOLMessage"
> 'ErrorHandler:$$N=basLMS.CreateOLMessage
> End Select
> ' End Error handling block.
> End Sub
>
> '================= END CODE ==================================[/color]


Alan Lane
Guest
 
Posts: n/a
#3: Nov 12 '05

re: Am I doing this correctly?


Hi Lauren:

I just see one little problem...If you get an error, what happens? Do
your objects get closed? No. Why? Because there is no "Resume ExitHere"
at the end of your Error Trap routine, to take processing back up to where
you close all your objects. Add "Resume ExitHere" (without quotes of
course) right above the "Exit Sub" line, and it should work correctly. Good
luck.

Alan


LaurenW
Guest
 
Posts: n/a
#4: Nov 12 '05

re: Am I doing this correctly?



Thanks Wayne. I had a suspicion I was overlooking something here.


On Mon, 19 Jan 2004 04:14:44 GMT, "Wayne Morgan"
<comprev_gothroughthenewsgroup@hotmail.com> wrote:
[color=blue]
>You don't need to GoTo ExitHere right above the ExitHere routine because
>that is where you'll go next anyway. In your error handler, if the error is
>287 you Resume Next, but if the error is Case Else you give a message and
>drop out the bottom of the sub. You should put in a Resume ExitHere before
>the End Sub so that you go back up and clean up your variables. At the start
>of your ExitHere routine you may want to use On Error Resume Next, that way
>if you get to the ExitHere (due to an error that was run through the error
>routine) be fore you Open something, such as objMessage, you won't receive
>an error that it can't close the unopened item.
>
> GoTo ExitHere 'Remove this
>
>ExitHere:
> On Error Resume Next
> objMsg.Close
> Set objMsg = Nothing
> objOutlook.Close
> Set objOutlook = Nothing
> ns.logoff
> Set ns = Nothing
>
> Exit Sub
>
>' Error handling block added by Error Handler Add-In. DO NOT EDIT this
>block of code.
>' Automatic error handler last updated at 11-12-2002 17:34:33
>'ErrorHandler:$$D=11-12-2002 'ErrorHandler:$$T=17:34:33
>HandleErr:
> Select Case Err.Number
> Case 287
> Resume Next
> Case Else
> MsgBox "Error " & Err.Number & ": " & Err.Description,
>vbCritical, "basLMS.CreateOLMessage"
>'ErrorHandler:$$N=basLMS.CreateOLMessage
> End Select
>' End Error handling block.
>Resume ExitHere
>End Sub[/color]

Pink Panther
Guest
 
Posts: n/a
#5: Nov 12 '05

re: Am I doing this correctly?


Hi Lauren,

In addition to the other comments

If IsOutlook = 0 Then
ErrMsg NoOutlookMsg
Exit Sub
End If

Needs to be

If IsOutlook = 0 Then
ErrMsg NoOutlookMsg
'make sure all your code exits through one point in a procedure!
'ie the Exit_Here block
Goto Exit_Here
End If

also

ExitHere:
On Error Resume Next
'it's good habit IMO to close/release your objects in reverse order
'to that which you created them
'ie objOutlook was instantiated first so release last
objMsg.Close
Set objMsg = Nothing
ns.logoff
Set ns = Nothing
objOutlook.Close
Set objOutlook = Nothing

Exit Sub

I've never seen a problem caused by not releasing in order (or by not
cleaning up) but you can never have too many good habit's I reckon.

Regards,

Peter
LaurenW
Guest
 
Posts: n/a
#6: Nov 12 '05

re: Am I doing this correctly?



Thanks! You guys are great!


On 19 Jan 2004 20:58:32 +1100, Pink Panther <PinkPanther2003@mail.com>
wrote:
[color=blue]
>Hi Lauren,
>
>In addition to the other comments
>
>If IsOutlook = 0 Then
> ErrMsg NoOutlookMsg
> Exit Sub
>End If
>
>Needs to be
>
>If IsOutlook = 0 Then
> ErrMsg NoOutlookMsg
> 'make sure all your code exits through one point in a procedure!
> 'ie the Exit_Here block
> Goto Exit_Here
>End If
>
>also
>
>ExitHere:
> On Error Resume Next
> 'it's good habit IMO to close/release your objects in reverse order
> 'to that which you created them
> 'ie objOutlook was instantiated first so release last
> objMsg.Close
> Set objMsg = Nothing
> ns.logoff
> Set ns = Nothing
> objOutlook.Close
> Set objOutlook = Nothing
>
> Exit Sub
>
>I've never seen a problem caused by not releasing in order (or by not
>cleaning up) but you can never have too many good habit's I reckon.
>
>Regards,
>
>Peter[/color]

David W. Fenton
Guest
 
Posts: n/a
#7: Nov 12 '05

re: Am I doing this correctly?


comprev_gothroughthenewsgroup@hotmail.com (Wayne Morgan) wrote in
<UQIOb.10144$N97.6294@newssvr16.news.prodigy.com >:
[color=blue]
>You don't need to GoTo ExitHere right above the ExitHere routine
>because that is where you'll go next anyway. In your error
>handler, if the error is 287 you Resume Next, but if the error is
>Case Else you give a message and drop out the bottom of the sub.
>You should put in a Resume ExitHere before the End Sub so that you
>go back up and clean up your variables. At the start of your
>ExitHere routine you may want to use On Error Resume Next, that
>way if you get to the ExitHere (due to an error that was run
>through the error routine) be fore you Open something, such as
>objMessage, you won't receive an error that it can't close the
>unopened item.
>
>ExitHere:
> On Error Resume Next
> objMsg.Close
> Set objMsg = Nothing
> objOutlook.Close
> Set objOutlook = Nothing
> ns.logoff
> Set ns = Nothing
>
> Exit Sub[/color]

I would never do that (On Error Resume Next) because my experience
is that if you don't set it back on, it sometimes remains off.

Instead, I'd test if the objects are Nothing in order to see if
they need to be cleaned up:

ExitHere:
If Not (objMsg Is Nothing) Then
objMsg.Close
Set objMsg = Nothing
End If
If Not (objOutlook Is Nothing) Then
objOutlook.Close
Set objOutlook = Nothing
End If
ns.logoff
Set ns = Nothing
Exit Sub

Or you could do something like Steve Jorgensen does and have a
subroutine that does this for you, and pass the object variable to
it. In that subroutine, you could have error handling appropriate
to the particular errors you might encounter in that particular
block, so that any errors would get handled appropriately without
getting you in a loop.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Closed Thread