473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Am I doing this correctly?

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
6 1479
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
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

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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: CGuy | last post by:
URGENT HELP REQUIRED FROM GURUS Hi, I have a custom object that implements ICollection and IListSource. This object has also an enumerator defined for it which implements IEnumerator and...
1
by: Viken Karaguesian | last post by:
Hello all, I'm taking a shot at table-less design, but I feel like I'm poking in the dark here. I decided to try and recreate the main page of a webiste that I run, www.sayatnova.com, using CSS...
1
by: Chiller | last post by:
Ok, I've implemented a few changes to the code and the bool functions now seem to be functioning correctly; however, I think I'm doing the convertions incorrectly because the values printed out...
1
by: Kevin | last post by:
Hi All Can someone tell me if I am doing this correctly, or can possibly suggest better ways, if I'm not doing this correctly. I have a windows application that I am writing,So I have a UI and...
8
by: xanthviper | last post by:
Hey, I'm trying to do what I think is pretty basic here, but I am missing something. Hopefully someone can point me in the right direction. Essentially I have an enumeration set up as the...
20
by: Frank Millman | last post by:
Hi all I am writing a multi-user accounting/business application, which uses sockets to communicate between client and server. I want to offer the option of encrypting the traffic between the...
28
by: fred.haab | last post by:
Not having server side scripting, I've been doing this for "last modified" tags on my pages: <div class="modified"> <script type="Text/JavaScript"> <!-- document.write("This page was last...
1
by: Jsuboy23 | last post by:
Can anyone review my code and see if I am setting the private variables correctly in the following? I have a Shop class that has a function that calls for infomation to be read from an external...
6
by: r035198x | last post by:
I have put together this article to give people starting Swing an awareness of issues that need to be considered when creating a Swing application. Most of the Swing tutorials that I have seen just...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.