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
 

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

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.
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,799
Messages
5,513,493
Members
408,954
Latest member
Tiaame

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top