Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using VB to send an email, and then save it as a MSG file

Posted on 2006-07-18
3
Medium Priority
?
272 Views
Last Modified: 2010-04-30
I have created a CRM application that manages all outgoing emails etc..
The application creates an email, populates the Email address and then waits for the user to complete the email and then send it.
Once it has sent the program then looks in the sent items and takes the latest item and saves it as a MSG file else where.
In principle this is sound and works well.

However it has become apparent that should the user cancel the email by clicking the top right hand cross the application just hangs (See * in the code below)
I realise that the code is not the best it could be, and was wondering if anyone has any ideas as to how I could handle this.
Details:
Language: VB6
Platform: XPpro SP2
MAPI Client: Outlook 2003
Exchange 2003


Here is the code:

Dim objOutlook As New Outlook.Application
Dim objNamespace As Outlook.NameSpace
Dim objmail As MailItem
Dim objnewmail As MailItem
Dim objFolder As Outlook.mapiFolder
Dim objOutbox As mapiFolder

Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objnewmail = objOutlook.CreateItem(olMailItem)
objnewmail.To = txtEmailAddress.Text
objnewmail.Display

On Error GoTo email_sent
'* The following three lines are where my application hangs should a user cancel the email instead of sending it
Do While Not objnewmail.Application Is Nothing
    Sleep 1000
Loop

email_sent:

Set objOutbox = objNamespace.GetDefaultFolder(olFolderOutbox)
If objOutbox.Items.Count > 0 Then Sleep 5000
If objOutbox.Items.Count > 0 Then Sleep 5000
If objOutbox.Items.Count > 0 Then Sleep 5000
If objOutbox.Items.Count > 0 Then Sleep 5000
Do While objOutbox.Items.Count > 0
 If MsgBox("Items are still present in your outbox, ensure that you are connected to your network, and that email facilities are working. Click OK to continue trying or Cancel to abort email import", vbOKCancel) = vbCancel Then Exit Sub
Loop

Set objFolder = Outlook.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail)

DoEvents
DoEvents

Set objmail = objFolder.Items.GetFirst
Set objmail = objFolder.Items.GetLast
'Set objmail = objFolder.Items(objFolder.Items.Count)

objmail.SaveAs gDocPath & dblComm & ".msg"

0
Comment
Question by:Craig_Edmondson
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
David Lee earned 2000 total points
ID: 17151262
Greetings, Craig_Edmondson.

I'd recommend a different approach.  First, the idea of looking at the most recent item in Sent Items isn't safe.  The user might send a regular message between the CRM send and the checking Sent Items.  The CRM app would then check Sent Items and find the wrong message.  Second, sleeping the app seems cumbersome to me.  Instead, I'd set a watch on Sent Items and forget about sleeping or checking the newest item in the folder.  The watch will fire each time a new item hits Sent Items.  To make sure you get the right item, i'd set a value in the message that you can test when the watch fires.  Here's a quick snippet of code showing how you can watch Sent Items.  Be sure and include a reference to the Outlook library in your project.

Private olkApp As Outlook.Application, _
    olkNS As Outlook.NameSpace, _
    WithEvents olkSentItems As Outlook.Items

Private Sub Command1_Click()
    Set olkApp = CreateObject("Outlook.Application")
    Set olkNS = olkApp.GetNamespace("MAPI")
    olkNS.Logon "Outlook"
    Set olkSentItems = olkApp.Session.GetDefaultFolder(olFolderSentMail).Items
    Debug.Print "Running"
End Sub

Private Sub Command2_Click()
    Set olkSentItems = Nothing
    olkNS.Logoff
    Set olkNS = Nothing
    Set olkApp = Nothing
    Debug.Print "Stopping"
End Sub

Private Sub olkSentItems_ItemAdd(ByVal Item As Object)
    Debug.Print Now & " " & Item.Subject
End Sub


Cheers!
0
 

Author Comment

by:Craig_Edmondson
ID: 17160684
Thanks for the help, Really apreciate it.

Never thought to use Events.

Cheers
0
 
LVL 76

Expert Comment

by:David Lee
ID: 17161027
You're welcome.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question