Solved

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

Posted on 2006-07-18
3
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

752 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