joyacv2
asked on
Sending emails using outlook without defining TO
Hi,
I am using this code to send emails using outlook. I need to modify this code to open outlook with all completed except the TO statement. If i left it in blank, then gives an error! Any idea?
I am using this code to send emails using outlook. I need to modify this code to open outlook with all completed except the TO statement. If i left it in blank, then gives an error! Any idea?
On Error Resume Next
Dim objOL As Object
'check if Outlook is running
Set objOL = GetObject(, "Outlook.Application")
'on error start Outlook
If Err.Number <> 0 Then '
Err.Clear
Shell "C:\Program Files\Microsoft Office\Office14\outlook.exe", vbMinimizedFocus
DoEvents
End If
Set olApp = CreateObject("Outlook.Application")
Set olMsg = olApp.CreateItem(0)
With olMsg
.Subject = "Subject"
.body = "Bodytext"
.To = ""
'.CC = Recipient
.Attachments.Add "c:\test.xlsx"
.Attachments.Add "c:\test2.xlsx"
.Send
End With
FIN:
Set olMsg = Nothing
Set olApp = Nothing
What is the error?
ASKER
the TO field, cannot be blank
Try it like this:
Sub OutlookMailer()
Dim objOL As Object, olMsg As Object
'check if Outlook is running
Set objOL = GetObject(, "Outlook.Application")
'on error start Outlook
If Err.Number <> 0 Then '
Set objOL = CreateObject("Outlook.Application")
End If
Set olMsg = objOL.CreateItem(0)
With olMsg
.Subject = "Subject"
.body = "Bodytext"
.To = ""
'.CC = Recipient
.Attachments.Add "c:\test.xlsx"
.Attachments.Add "c:\test2.xlsx"
.display
End With
Set olMsg = Nothing
Set objOL = Nothing
End Sub
I changed your code to use olMsg.Display rather than olMsg.Send. The .Display method brings up an Outlook window so you can enter the To address manually. The .Send method tries to send the email--which will obviously fail without an address.
I also corrected some consistency errors regarding objOL (you sometimes used olApp in its place).
I also used late-binding to create a reference to Outlook if it wasn't already running. You hard-coded a path to the application, which means that someone using a different version will break the macro.
I also corrected some consistency errors regarding objOL (you sometimes used olApp in its place).
I also used late-binding to create a reference to Outlook if it wasn't already running. You hard-coded a path to the application, which means that someone using a different version will break the macro.
Why do you not want the 'To' field to be blank?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
perfect!