Avatar of Justin
Justin

asked on 

How to create a Distribution list in Outlook e-mail from Excel Macro?

Hi Guys, I have a Macro which creates an attachment in Outlook from Excel, which uses the Code below:
What is the Code to populate the "To" field in Outlook so I don't have to manually type the e-mail distribution list every time?

"Public Sub CreateSendMail()

 Sheets(Array("Synth", "HistoChart", "LiqProv", "90-8DayDelta", "LiqProvDetail", "LiqProvUSD", "ClientMaturitiesProfile", "LiqProvGBP", _
        "LiqProvEUR")).Copy
  ChDir ThisWorkbook.Worksheets("ProviderAnalysisSetUp").Range("b3").Value
    ActiveWorkbook.SaveAs Filename:= _
        ThisWorkbook.Worksheets("ProviderAnalysisSetUp").Range("b3").Value & "LiqClientProviderLON.xlsx", FileFormat:= _
        xlOpenXMLWorkbook, CreateBackup:=False
    Application.Dialogs(xlDialogSendMail).Show"

End Sub
Provider-Copy.xlsm
Microsoft ExcelOutlookVisual Basic Classic

Avatar of undefined
Last Comment
Excel amusant
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try

 Application.Dialogs(xlDialogSendMail).Show arg1:="myMail@google.com;asas@yahoo.com", arg2:="This goes in the subject line"

Open in new window

Regards
You can use the following code..

Public Sub CreateSendMail()
Application.DisplayAlerts = False
Application.ScreenUpdating = False

Dim wb As Workbook
Dim wb1 As Workbook
Set wb1 = ThisWorkbook
Sheets(Array("Synth", "HistoChart", "LiqProv", "90-8DayDelta", "LiqProvDetail", "LiqProvUSD", "ClientMaturitiesProfile", "LiqProvGBP", _
        "LiqProvEUR")).Copy
Set wb = ActiveWorkbook
  ChDir wb1.Worksheets("ProviderAnalysisSetUp").Range("b3").Value
    wb.SaveAs Filename:= _
        wb1.Worksheets("ProviderAnalysisSetUp").Range("b3").Value & "LiqClientProviderLON.xlsx", FileFormat:= _
        xlOpenXMLWorkbook, CreateBackup:=False
 Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
     On Error Resume Next
    With OutMail
        .to = "ron@debruin.nl"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .Body = "Hi there"
        .Attachments.Add wb1.Worksheets("ProviderAnalysisSetUp").Range("b3").Value & "LiqClientProviderLON.xlsx"
        .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub

Open in new window


I picked up the code syntax from Here which to me personally best site for email codes like this...

Saurabh...
Avatar of Excel amusant
Excel amusant

If you are sending the file to multiple recipients then  enter the email addresses in the excel worksheets (Email Account).

and then click on the macro button "Send email".

Please see attached example file.
Provider-Copy--2-.xlsm
Avatar of Justin
Justin

ASKER

Hi RGonzo, how do you cc with your code? I can only send about 6 e-mail addresses in to "To" filed before it gives me an error
and I have 17 e-mail addresses on the mailing list?

 Application.Dialogs(xlDialogSendMail).Show arg1:="myMail@google.com;asas@yahoo.com", arg2:="This goes in the subject line"
JCutcliffe,

Did you try the code that i gave to you..You can add CC,BCC as well in the same..

Saurabh...
Avatar of Justin
Justin

ASKER

Hi Saurabh, yes I did but it's not attaching the file to the e-mail. Please help?
ASKER CERTIFIED SOLUTION
Avatar of Saurabh Singh Teotia
Saurabh Singh Teotia
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Justin
Justin

ASKER

Just tried it and I get an Error Message saying "Compile Error: Invalid or unqualified reference" on this line:

.Attachments.Add wb1.Worksheets("ProviderAnalysisSetUp").Range("b3").Value & "LiqClientProviderLON.xlsx"
Avatar of Excel amusant
Excel amusant

Hi,

Did you try my code, it allow you send to multiple recipients,

Attached please find the latest code that now attached the file too.
Provider-Copy--2-.xlsm
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo