Send email using spreadsheet

Status
Not open for further replies.
Y

YXR3b3Jr

Hello,

I am trying to send an email from a spread sheet. Below is what i have done

so far but what I want to do is select the values in A1 (A1 is the email

address) and send the email then select the value in A2 (A2 is the next email

address) and send the email and so on but i have to stop after sending 200

emails even though i have about 3000 in the excel spread sheet. The cc

address and bcc address will be the same every time.

Sub SENDMAIL()

Dim olApp As Object

Dim olNs As Object

Dim olMail As Object

Dim Email_Address

Dim ExcApp As Object

Dim Excval As String

Set ExcApp = Application.CreateObject("excel.application")

Set ExcWb = ExcApp.Workbooks.Open("C:\users\XXX\mailing\send.xls")

Excval = ExcWb.sheets("sheet1").Range("A1").Value

On Error Resume Next

Set olApp = GetObject(, "Outlook.Application")

Set olNs = olApp.GetNamespace("MAPI")

Set olMail = olApp.CreateItem(olMailItem)

With olMail

> To = Excval

> CC = "XXX@XXX.net"

'.BCC = "XXX@XXX.org"

> Subject = "We test and test"

> Body = "another test two"

> Attachments.Add "c:\users\XXX\Mailing\test.txt"

> Send

End With

Set olMail = Nothing

Set olApp = Nothing

End Sub
 
J

JP

Where are you running this code? Outlook, or Excel? You are using

CreateObject and GetObject for both Excel and Outlook, so it isn't

clear where the code is being run.

You should be using each application's native object references. i.e.

if you are running this from Outlook, try

Dim olApp As Outlook.Application

Set olApp = Outlook.Application

And if you run it from Excel, it should be

Dim ExcApp As Excel.Application

Set ExcApp = Excel.Application

Also, what you probably want to do is create a Range Object consisting

of all the values in column A (I assume the email addresses start in

A1 and run down continuously in the same column without blanks). Use a

For Each loop to process the contents of each cell in the range.

HTH

On Mar 12, 9:10 pm, atwork <atw...> wrote:
> Hello,
> I am trying to send an email from a spread sheet. Below is what i have done
> so far but what I want to do is select the values in A1 (A1 is the email
> address) and send the email then select the value in A2 (A2 is the next email
> address) and  send the email and so on but i have to stop after sending200
> emails even though i have about 3000 in the excel spread sheet. The cc
> address and bcc address will be the same every time.
> Sub SENDMAIL()
>     Dim olApp As Object
>     Dim olNs As Object
>     Dim olMail As Object
>     Dim Email_Address
>     Dim ExcApp As Object
>     Dim Excval As String
>     Set ExcApp = Application.CreateObject("excel.application")
>     Set ExcWb = ExcApp.Workbooks.Open("C:\users\XXX\mailing\send.xls")
>     Excval = ExcWb.sheets("sheet1").Range("A1").Value
>      On Error Resume Next
>     Set olApp = GetObject(, "Outlook.Application")
>     Set olNs = olApp.GetNamespace("MAPI")
>     Set olMail = olApp.CreateItem(olMailItem)
>     With olMail
>         .To = Excval
>         .CC = "X...@XXX.net"
>         '.BCC = "X...@XXX.org"
>         .Subject = "We test and test"
>         .Body = "another test two"
>         .Attachments.Add "c:\users\XXX\Mailing\test.txt"
>         .Send
>     End With
>     Set olMail = Nothing
>     Set olApp = Nothing
> End Sub
 
Y

YXR3b3Jr

JP,

I am running this from Outlook and you are correct the addresses start at A1

and run down the column. I am a little confused with setting up the loop.

"JP" wrote:


> Where are you running this code? Outlook, or Excel? You are using
> CreateObject and GetObject for both Excel and Outlook, so it isn't
> clear where the code is being run.

> You should be using each application's native object references. i.e.
> if you are running this from Outlook, try

> Dim olApp As Outlook.Application
> Set olApp = Outlook.Application

> And if you run it from Excel, it should be

> Dim ExcApp As Excel.Application
> Set ExcApp = Excel.Application

> Also, what you probably want to do is create a Range Object consisting
> of all the values in column A (I assume the email addresses start in
> A1 and run down continuously in the same column without blanks). Use a
> For Each loop to process the contents of each cell in the range.

> HTH

> On Mar 12, 9:10 pm, atwork <atw...> wrote:
> > Hello,
> > I am trying to send an email from a spread sheet. Below is what i have done
> > so far but what I want to do is select the values in A1 (A1 is the email
> > address) and send the email then select the value in A2 (A2 is the next email
> > address) and send the email and so on but i have to stop after sending 200
> > emails even though i have about 3000 in the excel spread sheet. The cc
> > address and bcc address will be the same every time.
> > Sub SENDMAIL()
> > Dim olApp As Object
> > Dim olNs As Object
> > Dim olMail As Object
> > Dim Email_Address
> > Dim ExcApp As Object
> > Dim Excval As String
> > Set ExcApp = Application.CreateObject("excel.application")
> > Set ExcWb = ExcApp.Workbooks.Open("C:\users\XXX\mailing\send.xls")
> > Excval = ExcWb.sheets("sheet1").Range("A1").Value
> > On Error Resume Next
> > Set olApp = GetObject(, "Outlook.Application")
> > Set olNs = olApp.GetNamespace("MAPI")
> > Set olMail = olApp.CreateItem(olMailItem)
> > With olMail
> > .To = Excval
> > .CC = "X...@XXX.net"
> > '.BCC = "X...@XXX.org"
> > .Subject = "We test and test"
> > .Body = "another test two"
> > .Attachments.Add "c:\users\XXX\Mailing\test.txt"
> > .Send
> > End With
> > Set olMail = Nothing
> > Set olApp = Nothing
> > End Sub


>
 
J

JP

You'll want to set up the loop after the line of code that opens the

workbook. Here's some air code for that, it's not the way I described

earlier but it should get you started. This code simply selects each

cell in column A in turn, runs some code, then selects the next cell

down, and continues to do so until there are no more values in column

A.

Do While ActiveCell.Value <> ""

' your code here to send email, ActiveCell will contain email

address

ActiveCell.Offset(1,0).Select

Loop

The code assumes that when the workbook opens, cell A1 is selected

(which is usually the case in my experience).

As I mentioned earlier, since you are doing this from Outlook, you

have direct access to Outlook's object model, so you should be using

Outlook.Application, not GetObject, for your olApp variable. The

Outlook objects should be appropriately declared (Outlook.Application,

Outlook.Namespace, Outlook.MailItem, etc).

Unfortunately I have to step outside now but if you like I can also

post another version that uses a Variant to read all the email

addresses in one pass, which should make the code a bit faster.

--JP

On Mar 13, 10:36 am, atwork <atw...> wrote:
> JP,
> I am running this from Outlook and you are correct the addresses start atA1
> and run down the column. I am a little confused with setting up the loop.

> "JP" wrote:
> > Where are you running this code? Outlook, or Excel? You are using
> > CreateObject and GetObject for both Excel and Outlook, so it isn't
> > clear where the code is being run.

>
 
Y

YXR3b3Jr

JP,

Thanks for all your input. I could not figure out how to stop the sending

after 200 messages using the do while loop. Therefore, I did some more

research and found another way to deal with the cells. Did I make the

appropriate change for the Outlook object model? Here is what I think I am

going to go with:

Sub SENDMAIL()

Dim olApp As Outlook.Application

Dim olNs

Dim olMail

Dim Email_Address

Dim ExcApp As Object

Dim Excval As String

Set ExcApp = Application.CreateObject("excel.application")

Set ExcWb =

ExcApp.Workbooks.Open("C:\users\dwesterfield\mailing\send.xls")

For i = 1 To 200

Excval = ExcWb.ActiveSheet.Cells(i, 1)

'On Error Resume Next

Set olApp = Outlook.Application

Set olNs = olApp.GetNamespace("MAPI")

Set olMail = olApp.CreateItem(olMailItem)

With olMail

> To = Excval

'.CC = "dwest@goeaston.net"

'.BCC = "membership@mcuf.org"

> Subject = "We test and test and test some more"

> Body = "Please read the attachment"

> Attachments.Add "c:\users\dwesterfield\Mailing\test.txt"

> Send

End With

Next

Set olMail = Nothing

Set olApp = Nothing

ExcApp.Quit

End Sub

"JP" wrote:


> You'll want to set up the loop after the line of code that opens the
> workbook. Here's some air code for that, it's not the way I described
> earlier but it should get you started. This code simply selects each
> cell in column A in turn, runs some code, then selects the next cell
> down, and continues to do so until there are no more values in column
> A.

> Do While ActiveCell.Value <> ""
> ' your code here to send email, ActiveCell will contain email
> address

> ActiveCell.Offset(1,0).Select
> Loop

> The code assumes that when the workbook opens, cell A1 is selected
> (which is usually the case in my experience).

> As I mentioned earlier, since you are doing this from Outlook, you
> have direct access to Outlook's object model, so you should be using
> Outlook.Application, not GetObject, for your olApp variable. The
> Outlook objects should be appropriately declared (Outlook.Application,
> Outlook.Namespace, Outlook.MailItem, etc).

> Unfortunately I have to step outside now but if you like I can also
> post another version that uses a Variant to read all the email
> addresses in one pass, which should make the code a bit faster.

> --JP

> On Mar 13, 10:36 am, atwork <atw...> wrote:
> > JP,
> > I am running this from Outlook and you are correct the addresses start at A1
> > and run down the column. I am a little confused with setting up the loop.
> > "JP" wrote:
> > > Where are you running this code? Outlook, or Excel? You are using
> > > CreateObject and GetObject for both Excel and Outlook, so it isn't
> > > clear where the code is being run.

> >

>
 
J

JP

You'll want to change

Dim olNs

Dim olMail

to

Dim olNs As Outlook.Namespace

Dim olMail As Outlook.MailItem

It doesn't look like you're using the variable Email_Address anywhere

in your procedure, so I think you can safely remove it. And you

haven't declared variable i. Overall it looks correct, but you've

hardcoded the loop counter. When you add or remove email addresses

from column A, you'll need to revise the loop.

--JP

On Mar 14, 5:31 pm, atwork <atw...> wrote:
> JP,
> Thanks for all your input. I could not figure out how to stop the sending
> after 200 messages using the do while loop. Therefore, I did some more
> research and found another way to deal with the cells. Did I make the
> appropriate change for the Outlook object model? Here is what I think I am
> going to go with:

> Sub SENDMAIL()
>     Dim olApp As Outlook.Application
>     Dim olNs
>     Dim olMail
>     Dim Email_Address
>     Dim ExcApp As Object
>     Dim Excval As String
>     Set ExcApp = Application.CreateObject("excel.application")
>     Set ExcWb =
> ExcApp.Workbooks.Open("C:\users\dwesterfield\mailing\send.xls")
>         For i = 1 To 200
>         Excval = ExcWb.ActiveSheet.Cells(i, 1)
>  'On Error Resume Next
>         Set olApp = Outlook.Application
>         Set olNs = olApp.GetNamespace("MAPI")
>         Set olMail = olApp.CreateItem(olMailItem)
>             With olMail
>             .To = Excval
>             '.CC = "dw...@goeaston.net"
>             '.BCC = "members...@mcuf.org"
>             .Subject = "We test and test and test some more"
>             .Body = "Please read the attachment"
>             .Attachments.Add "c:\users\dwesterfield\Mailing\test.txt"
>             .Send
>         End With
>     Next
>     Set olMail = Nothing
>     Set olApp = Nothing
>     ExcApp.Quit
> End Sub
>
 
Y

YXR3b3Jr

JP,

I will make those changes but I just wanted to say thanks for the excellent

help!!

"JP" wrote:


> You'll want to change

> Dim olNs
> Dim olMail

> to

> Dim olNs As Outlook.Namespace
> Dim olMail As Outlook.MailItem

> It doesn't look like you're using the variable Email_Address anywhere
> in your procedure, so I think you can safely remove it. And you
> haven't declared variable i. Overall it looks correct, but you've
> hardcoded the loop counter. When you add or remove email addresses
> from column A, you'll need to revise the loop.

> --JP

> On Mar 14, 5:31 pm, atwork <atw...> wrote:
> > JP,
> > Thanks for all your input. I could not figure out how to stop the sending
> > after 200 messages using the do while loop. Therefore, I did some more
> > research and found another way to deal with the cells. Did I make the
> > appropriate change for the Outlook object model? Here is what I think I am
> > going to go with:
> > Sub SENDMAIL()
> > Dim olApp As Outlook.Application
> > Dim olNs
> > Dim olMail
> > Dim Email_Address
> > Dim ExcApp As Object
> > Dim Excval As String
> > Set ExcApp = Application.CreateObject("excel.application")
> > Set ExcWb =
> > ExcApp.Workbooks.Open("C:\users\dwesterfield\mailing\send.xls")
> > For i = 1 To 200
> > Excval = ExcWb.ActiveSheet.Cells(i, 1)
> > 'On Error Resume Next
> > Set olApp = Outlook.Application
> > Set olNs = olApp.GetNamespace("MAPI")
> > Set olMail = olApp.CreateItem(olMailItem)
> > With olMail
> > .To = Excval
> > '.CC = "dw...@goeaston.net"
> > '.BCC = "members...@mcuf.org"
> > .Subject = "We test and test and test some more"
> > .Body = "Please read the attachment"
> > .Attachments.Add "c:\users\dwesterfield\Mailing\test.txt"
> > .Send
> > End With
> > Next
> > Set olMail = Nothing
> > Set olApp = Nothing
> > ExcApp.Quit
> > End Sub
> >

>
 
J

JP

No problem, and good luck to you!

On Mar 15, 10:12 am, atwork <atw...> wrote:
> JP,
> I will make those changes but I just wanted to say thanks for the excellent
> help!!

> "JP" wrote:
> > You'll want to change

>
> > Dim olNs
> > Dim olMail

>
> > to

>
> > Dim olNs As Outlook.Namespace
> > Dim olMail As Outlook.MailItem

>
> > It doesn't look like you're using the variable Email_Address anywhere
> > in your procedure, so I think you can safely remove it. And you
> > haven't declared variable i. Overall it looks correct, but you've
> > hardcoded the loop counter. When you add or remove email addresses
> > from column A, you'll need to revise the loop.

>
> > --JP

>
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
J Add an Attachment Using an Array and Match first 17 Letters to Matching Template .oft to Send eMail Outlook VBA and Custom Forms 2
D Using a VBA Custom Form to Send Reoccurring Email Upon Task Completion Outlook VBA and Custom Forms 4
D send email from Excel using outlook template Outlook VBA and Custom Forms 3
Diane Poremsky How to Send Email When Traveling or Using Public Wi-Fi New Slipstick.com Articles 0
H send reminder if no reply received on first or original email using macro Using Outlook 2
T Using a macro to send email to diffrent address Outlook VBA and Custom Forms 1
S Can't send messages using Outlook 2007 from a business email Using Outlook 3
SirFishAlot Send atachment with mass email using BCM, and Publisher 2013 BCM (Business Contact Manager) 1
M Unable to send email to gmail - using Outlook 2007 Using Outlook 3
C Error message when trying to send email using a distribution list Using Outlook 2
L Using a template for new email message, changes are not saved unless I send the message...using Wind Using Outlook 3
W Cannot send email using Outlook (MailTo problem) Using Outlook 1
C At a hotel in another counry--the wireless server won't allow me to send outgoing email messages. I am using the same settings I used at home. I can Using Outlook 2
P Unable to send email using Outlook 2010 using Verizon ISP Using Outlook 3
T Can't send email using "Attach to email" from Acrobat (and other programs) to Outlook Using Outlook 1
B After Updates last night (9/16/2010) I cannot send email using Outlook 2007 (q.com POP3 account) Using Outlook 1
O Outlook and Exchange 2007 send new/reply email using alias other than default ?? Using Outlook 2
K can't send an email using outlook 2003 help pls. Using Outlook 2
M 1. Split large videos or images into small chunks to send an email using Outlook 2007.__2. Maximum i Using Outlook 3
C Can send email but not receive it using Outlook 2007 Using Outlook 0
J send email using template Outlook VBA and Custom Forms 8
R Using SendObject in Access to send an email Outlook VBA and Custom Forms 3
M Extract all links from Outlook email, send to Excel Using Outlook 2
T After I send a new email, it remains in the Draft folder Using Outlook.com accounts in Outlook 3
S Change VBA script to send HTML email instead of text Outlook VBA and Custom Forms 3
M ERROR: None of your email accounts could send to this recipient Using Outlook 2
ChrisK2 Send email to advertise@slipstick.com fails: "The group advertising isn't set up to receive messages from..." Using Outlook 3
R auto send email when meeting closes from a shared calendar only Outlook VBA and Custom Forms 2
X Unable to send an email from one account to another on same PC Using Outlook 2
M VBA to send reminder email if no response Using Outlook 13
M Can't send email in outlook.com Using Outlook 9
B When working on emails in a certain folder, when I hit reply or reply all, I would like it re always reply all and add an email address to send to Outlook VBA and Custom Forms 3
A Outlook - Send New 20 Attachments through Email Using Outlook 4
S Send email via SMTP - use transport rules to add to senders inbox (then rule to move to sent items Exchange Server Administration 1
R Prompt asking the user to send email to folder as *.msg file Outlook VBA and Custom Forms 1
D Is it possible to automatically send an email when it is moved to a folder? Exchange Server Administration 1
B VBA Help Email that will save as draft and send as attachment Outlook VBA and Custom Forms 3
A Automatically send email based on drop-down field? Outlook VBA and Custom Forms 2
K Macro Not Executing then send email from Explorer Outlook VBA and Custom Forms 3
A Block user to send emails to specific set of email ids Using Outlook 1
T Can't send email through connected account (outlook.live.com) - goes to Drafts folder Using Outlook.com accounts in Outlook 3
C Custom Application Form send Email to Another User Using Outlook 1
C Macro to send email after changing from address and adding signature Outlook VBA and Custom Forms 1
M Send an Email When a Reminder Fires Outlook VBA and Custom Forms 1
Diane Poremsky Send an Email When You Add an Appointment to Your Calendar New Slipstick.com Articles 0
Diane Poremsky Send email to all addresses for one Contact New Slipstick.com Articles 0
G "Delay Delivery" Email fails to send if opened while in Outbox Using Outlook 4
E Send a Reminder/Task to certain Email Recipient Using Outlook 5
Diane Poremsky Send an Email When a Reminder Fires New Slipstick.com Articles 0
A Send email from BCM BCM (Business Contact Manager) 5
Similar threads


















































Top