afternoon all,
i have an excel macro that sends multiple emails (multiple calls to my mailer sub - thanks for the FAQ - it was of much help). the multiple calls to the mailer sub will pass different arguments based upon info in the spreadsheet.
the issue is that the outlook object does not seem to always have focus when the sendkeys line is executed.
any suggestions? i've already played with appactivate to no avail.
the application.wait does help but still not all of the time.
any and all suggestions are welcome
regards,
longhair
i have an excel macro that sends multiple emails (multiple calls to my mailer sub - thanks for the FAQ - it was of much help). the multiple calls to the mailer sub will pass different arguments based upon info in the spreadsheet.
the issue is that the outlook object does not seem to always have focus when the sendkeys line is executed.
any suggestions? i've already played with appactivate to no avail.
the application.wait does help but still not all of the time.
Code:
...
Call Mailer(txtSubject, txtPath, txtSendTo)
...
Sub Mailer(txtSubject As String, txtPath As String, txtSendTo As String)
Sheets("dsgm862").Select
pathname = txtPath 'defines attachment
Dim objol As New Outlook.Application
Dim objmail As MailItem
Set objol = New Outlook.Application
Set objmail = objol.createitem(olmailitem)
With objmail
.To = txtSendTo 'enter in here the email address
'.cc = "whoever" 'enter in here the email address
.Subject = txtSubject
'.Body = "Please find attached the teste email" & _
vbCrLf & "If you have any queries can you please let me know" & vbCrLf
.Attachments.Add pathname 'adds attachment to email
.display
End With
'Set objmail = Nothing
'Set objol = Nothing
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 2
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
SendKeys "%{s}", True 'send the email without prompts
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 3
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
Set objmail = Nothing
Set objol = Nothing
AppActivate "Microsoft Excel"
End Sub
regards,
longhair