Solved

Sending emails using outlook without defining TO

Posted on 2013-05-11
7
297 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

823 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