Solved

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

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now