By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,498 Members | 835 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,498 IT Pros & Developers. It's quick & easy.

Am I doing this correctly?

P: n/a
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 ==================================
Nov 12 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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:c4********************************@4ax.com...
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 ==================================

Nov 12 '05 #2

P: n/a
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
Nov 12 '05 #3

P: n/a

Thanks Wayne. I had a suspicion I was overlooking something here.
On Mon, 19 Jan 2004 04:14:44 GMT, "Wayne Morgan"
<co***************************@hotmail.com> wrote:
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


Nov 12 '05 #4

P: n/a
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
Nov 12 '05 #5

P: n/a

Thanks! You guys are great!
On 19 Jan 2004 20:58:32 +1100, Pink Panther <Pi*************@mail.com>
wrote:
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


Nov 12 '05 #6

P: n/a
co***************************@hotmail.com (Wayne Morgan) wrote in
<UQ******************@newssvr16.news.prodigy.com >:
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


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
Nov 12 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.