+ Reply to Thread
Results 1 to 3 of 3

If Then statement embedded in my Email Code

Hybrid View

Ikebears If Then statement embedded in... 05-21-2019, 05:31 PM
Pauleyb Re: If Then statement... 05-28-2019, 05:00 PM
AliGW Re: If Then statement... 05-28-2019, 05:04 PM
  1. #1
    Registered User
    Join Date
    05-21-2019
    Location
    US
    MS-Off Ver
    Excel
    Posts
    1

    Question If Then statement embedded in my Email Code

    Hey all, I'm trying to add an if then statement into my Email code. The if then statement is this:

    Sub ClearW1D1M1()
    If Sheet2.Range("B52").Value = 0 Then Sheet2.Range("A52:D52").ClearContents
    If Sheet2.Range("B53").Value = 0 Then Sheet2.Range("A53:D53").ClearContents
    End Sub
    
    And the full Email code is here: 
    
    Sub MailW1D1M1()
    Dim outlook As Object
    Dim newEmail As Object
    Dim xInspect As Object
    Dim pageEditor As Object
    Set outlook = CreateObject("Outlook.Application")
    Set newEmail = outlook.CreateItem(0)
    With newEmail
        .To = Sheet2.Range("B3")
        .CC = Sheet2.Range("D3")
        .BCC = ""
        .Subject = Sheet2.Range("B13")
        .display
        Set xInspect = newEmail.GetInspector
        Set pageEditor = xInspect.WordEditor
        Sheet2.Range("A45:D53").Copy
        pageEditor.Application.Selection.End = pageEditor.Application.Selection.Start
        pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)
        .display
        Set pageEditor = Nothing
        Set xInspect = Nothing
        .display
    End With
    Set newEmail = Nothing
    Set outlook = Nothing
    End Sub
    Pretty much I want the Email code to run the if then before populating the email to remove information that is not needed, but I keep getting errors. Any ideas would be much appreciated!

    Thanks
    Last edited by AliGW; 05-28-2019 at 05:03 PM.

  2. #2
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: If Then statement embedded in my Email Code

    The email code works until you add in the if/then statements? Where are you adding them? What error do you get?

    A quick glance shows the use of Sheet2.xxxx. I think that only works if Sheet2 is active, but then you would get errors in execution beyond the if/then statements. It would be better to use Sheets("Sheet2").xxxx
    Pauley
    --------
    If I helped with your issue, I'd appreciate a rep bump (hit the '*' icon to the bottom left of this post).

  3. #3
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2505 (Windows 11 Home 24H2 64-bit)
    Posts
    91,211

    Re: If Then statement embedded in my Email Code

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. As you are new here, I have done it for you this time.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Attaching an embedded PDF file in my worksheet to a new email
    By AnthonyGFS in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-14-2017, 04:24 AM
  2. Reading the text embedded in an email icon
    By soranz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-19-2015, 11:26 PM
  3. send an email with logo embedded
    By melody10 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-10-2014, 11:38 AM
  4. Create a new column with company name which is embedded in email name
    By ajang in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 06-13-2014, 01:10 AM
  5. Add embedded picture to email
    By Swift4Play in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-17-2011, 01:45 PM
  6. Embedded eMail Hyperlink
    By dumbnut in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-24-2010, 12:11 PM
  7. Need help with an embedded IF statement
    By Lari in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 06-17-2006, 12:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1