Help with my VB emailing code

csilabgirl

Active Member
Joined
Aug 14, 2009
Messages
359
Excel 2002
I use this code below and it works fine. Problem is, sometimes cell A8 does not have an email adress in it, but the user will still click the send button because they do not realize there is no email in the "to:" and it will send because there is an email address in the Bcc. I wanted an IF/THEN code that would check if the "to:" was blank and then give a message box telling them of the error. I added the following code If .Item.To = "" then msgbox ("Error"). But it did not work, if gives me the error msgbox regardless of it is blank or if it has an email address in it. Thanks for the help everyone.

ActiveSheet.Range("A1:G39").Select
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
.Item.To = ClearContents
.Item.To = Range("A8")
.Item.Subject = Range("A9")
.Item.Bcc = "SampleReceipt@chromosomal-labs.com"
End With
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
Maybe something like this (not tested):

<font face=Calibri><SPAN style="color:#00007F">Sub</SPAN> foo()<br>    <SPAN style="color:#00007F">Dim</SPAN> rng <SPAN style="color:#00007F">As</SPAN> Range<br>        <SPAN style="color:#00007F">Set</SPAN> rng = ActiveSheet.Range("A8")<br>        <br>        <SPAN style="color:#00007F">If</SPAN> LenB(rng) = 0 <SPAN style="color:#00007F">Then</SPAN><br>            MsgBox "No e-mail address entered!"<br>            rng.Select<br>            <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>        <SPAN style="color:#00007F">Else</SPAN><br>            ActiveSheet.Range("A1:G39").Select<br>            ActiveWorkbook.EnvelopeVisible = <SPAN style="color:#00007F">True</SPAN><br>                <SPAN style="color:#00007F">With</SPAN> ActiveSheet.MailEnvelope<br>                    <SPAN style="color:#00007F">With</SPAN> .Item<br>                        .To = .ClearContents<br>                        .To = rng<br>                        .Subject = rng.Offset(1)<br>                        <SPAN style="color:#007F00">'.Bcc = "SampleReceipt@chromosomal-labs.com"</SPAN><br>                    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

HTH,
 

csilabgirl

Active Member
Joined
Aug 14, 2009
Messages
359
When I try your code and I DONT have an email address in cell A8, I get the proper error msgbox, but if I do have an email address in cell A8, then I get a VB error box of Object doesnt support this property or method
 

csilabgirl

Active Member
Joined
Aug 14, 2009
Messages
359
I found the problem, the "." before clear contents in your code needs to be removed. Thanks for the code, it works now
 

csilabgirl

Active Member
Joined
Aug 14, 2009
Messages
359

ADVERTISEMENT

Actually there is one poblem, the clear contents is not working, but the main issue is I need it to clear the contents as the first thing it does prior to the error check.

This worksheet is sent over and over to different people, the email address in A8 changes with a macro. Even if A8 does not contain a new email address, the previous email address is still in the "to:". So even if there is a msgbox that pops up warning them of no email address in A8, there is still an email sitting in the "to:" and if they dont pay attention to my msgbox they could hit send and send the sheet to the wrong person.

So I need it to clear the contents of the "to:" first and then run the error check fro A8.
 

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
Just put the ClearContents portion as a stand-alone element before the msgbox.
 

csilabgirl

Active Member
Joined
Aug 14, 2009
Messages
359
I did try that before I submitted my reply, but it didnt work.

I had tried

ActiveSheet.Range("A1:G39").Select
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
With .Item
.To = ClearContents
End with

Maybe the clear contents function is not meant to work outside of cells?
 

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
Frankly, I'm not familiar with the MailEnvelope call, so I don't know how to respond to that. Why not try setting the To portion to = Nothing at the end of the code? That way you don't have to worry about it up front.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,616
Messages
5,832,716
Members
430,156
Latest member
Amosjack1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top