Solved

Sending emails using outlook without defining TO

Posted on 2013-05-11
7
303 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
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 46

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
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 Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

809 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