Hi All-
I needed to write a macro for a client that takes some data from excel and email merges from Outlook, both in 2003. I have written many macros similar to this and have never had a problem. However, with this client, it seems that the macro sends out two emails when every time I have tested it, it sends out a single email to each recipient (as it should). I have no idea what's going on so I tested the code in different Office 2003 service packs, but it still works fine in my environment. This is generic 2003 vba code where you create all drafts of the emails in outlook, and then send them one by one after all are created. here's the vba from excel. Any thoughts? Thanks in advance!
Sub GenerateEmailDrafts()
Dim i As Integer
Dim objApp As Outlook.Application
Dim l_Msg As MailItem
Dim wb_URL As Workbook
Dim wk_URL As Worksheet
Dim ErrorMessage As String
Dim CountURL As Integer
Dim Cell As Variant
Dim range_URL As String
On Error GoTo ErrorHandler
'Initialize variables
Set wb_URL = ThisWorkbook
Set wk_URL = ThisWorkbook.Sheets("Data")
ErrorMessage = "Please make sure Outlook is open and then run this step again."
Set objApp = CreateObject("Outlook.Application")
ErrorMessage = ""
CountURL = wk_URL.UsedRange.Rows.Count
range_URL = "A2:A" & CountURL
For Each Cell In wk_URL.Range(range_URL)
Set l_Msg = objApp.CreateItem(olMailItem)
l_Msg.To = Cell.Offset(0, 3).Value
l_Msg.Subject = EmailSubject
l_Msg.HTMLBody = EmailTemplate(Cell.Offset(0, 2).Value, Cell.Offset(0, 1).Value, Cell.Offset(0, 4).Value)
l_Msg.Save
l_Msg.Close (olSave)
Set l_Msg = Nothing
Next Cell
Set objApp = Nothing
'Success, send everything in drafts
Call sendDrafts(True)
'Close this Application
Application.Quit
ThisWorkbook.Close SaveChanges:=True
Exit Sub
'....
End Sub
Sub sendDrafts(RunNow As Boolean)
Dim objApp As Outlook.Application
Dim objNew As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder
Dim objNS As Outlook.Namespace, objItem As Outlook.MailItem
Set objApp = CreateObject("Outlook.Application")
Set objNS = Outlook.Application.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderDrafts)
On Error Resume Next
Do
For Each objItem In objInbox.Items
objApp.ActiveExplorer.Activate
Application.Wait (Now + TimeValue("0:00:02"))
objItem.Display
SendKeys String:="%s", Wait:=True
Next objItem
Set objItem = Nothing
Loop Until objInbox.Items.Count = 0
End Sub
I needed to write a macro for a client that takes some data from excel and email merges from Outlook, both in 2003. I have written many macros similar to this and have never had a problem. However, with this client, it seems that the macro sends out two emails when every time I have tested it, it sends out a single email to each recipient (as it should). I have no idea what's going on so I tested the code in different Office 2003 service packs, but it still works fine in my environment. This is generic 2003 vba code where you create all drafts of the emails in outlook, and then send them one by one after all are created. here's the vba from excel. Any thoughts? Thanks in advance!
Sub GenerateEmailDrafts()
Dim i As Integer
Dim objApp As Outlook.Application
Dim l_Msg As MailItem
Dim wb_URL As Workbook
Dim wk_URL As Worksheet
Dim ErrorMessage As String
Dim CountURL As Integer
Dim Cell As Variant
Dim range_URL As String
On Error GoTo ErrorHandler
'Initialize variables
Set wb_URL = ThisWorkbook
Set wk_URL = ThisWorkbook.Sheets("Data")
ErrorMessage = "Please make sure Outlook is open and then run this step again."
Set objApp = CreateObject("Outlook.Application")
ErrorMessage = ""
CountURL = wk_URL.UsedRange.Rows.Count
range_URL = "A2:A" & CountURL
For Each Cell In wk_URL.Range(range_URL)
Set l_Msg = objApp.CreateItem(olMailItem)
l_Msg.To = Cell.Offset(0, 3).Value
l_Msg.Subject = EmailSubject
l_Msg.HTMLBody = EmailTemplate(Cell.Offset(0, 2).Value, Cell.Offset(0, 1).Value, Cell.Offset(0, 4).Value)
l_Msg.Save
l_Msg.Close (olSave)
Set l_Msg = Nothing
Next Cell
Set objApp = Nothing
'Success, send everything in drafts
Call sendDrafts(True)
'Close this Application
Application.Quit
ThisWorkbook.Close SaveChanges:=True
Exit Sub
'....
End Sub
Sub sendDrafts(RunNow As Boolean)
Dim objApp As Outlook.Application
Dim objNew As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder
Dim objNS As Outlook.Namespace, objItem As Outlook.MailItem
Set objApp = CreateObject("Outlook.Application")
Set objNS = Outlook.Application.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderDrafts)
On Error Resume Next
Do
For Each objItem In objInbox.Items
objApp.ActiveExplorer.Activate
Application.Wait (Now + TimeValue("0:00:02"))
objItem.Display
SendKeys String:="%s", Wait:=True
Next objItem
Set objItem = Nothing
Loop Until objInbox.Items.Count = 0
End Sub