Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-07-18
3
Medium Priority
?
269 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

705 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