Solved

Sending emails using outlook without defining TO

Posted on 2013-05-11
7
307 Views
Last Modified: 2013-05-11
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?

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

Open in new window

0
Comment
Question by:joyacv2
[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
7 Comments
 
LVL 12

Expert Comment

by:mlongoh
ID: 39158609
What is the error?
0
 
LVL 1

Author Comment

by:joyacv2
ID: 39158629
the TO field, cannot be blank
0
 
LVL 81

Expert Comment

by:byundt
ID: 39158642
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

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 81

Expert Comment

by:byundt
ID: 39158644
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.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 39158647
Why do you not want the 'To' field to be blank?
0
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39158651
I found a runtime error if Outlook was not already running. The version below fixes that error.
Sub OutlookMailer()
Dim objOL As Object, olMsg As Object
    
    'check if Outlook is running
On Error Resume Next
Set objOL = GetObject(, "Outlook.Application")
On Error GoTo 0

    'on error start Outlook
If objOL Is Nothing Then Set objOL = CreateObject("Outlook.Application")

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

Open in new window

0
 
LVL 1

Author Closing Comment

by:joyacv2
ID: 39158657
perfect!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

763 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