Solved

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

Posted on 2006-07-18
3
239 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 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

786 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