+ Reply to Thread
Results 1 to 3 of 3

Need help in email code

  1. #1
    Registered User
    Join Date
    02-11-2017
    Location
    Singapore
    MS-Off Ver
    10
    Posts
    98

    Lightbulb Need help in email code

    Hi
    I am working on a code to send email with details from tracker in html format. But when I am trying to include condition and details from other sheet its not working. I wanted to include below details
    If Range ("I" & i) = "N" Then

    And also wanted to place the details in Sheet 2

    Dim StrBody As String
    HTMLBody = StrBody & RangetoHTML(rng)

    StrBody = Sheets("Sheet2").Range("A1").Value & "<br>" & _
    Sheets("Sheet2").Range("A2").Value & "<br>" & _
    Sheets("Sheet2").Range("A3").Value & "<br><br><br>"


    The below code is working fine but when I am trying to insert the above code it’s not working.

    Sub Mail_Sheet_Outlook_Body()

    Dim rng As Range
    Dim StrBody As String
    Dim OutApp As Object
    Dim OutMail As Object

    With Application
    .EnableEvents = False
    .ScreenUpdating = False
    End With

    Set rng = Nothing
    Set rng = ActiveSheet.UsedRange

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
    .To = "[email protected]"
    .CC = ""
    .BCC = ""
    .Subject = "This is the Subject line"
    .HTMLBody = RangetoHTML(rng)
    .Send
    End With
    On Error GoTo 0

    With Application
    .EnableEvents = True
    .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
    End Sub


    Function RangetoHTML(rng As Range)

    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"


    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
    .Cells(1).PasteSpecial Paste:=8
    .Cells(1).PasteSpecial xlPasteValues, , False, False
    .Cells(1).PasteSpecial xlPasteFormats, , False, False
    .Cells(1).Select
    Application.CutCopyMode = False
    On Error Resume Next
    .DrawingObjects.Visible = True
    .DrawingObjects.Delete
    On Error GoTo 0
    End With


    With TempWB.PublishObjects.Add( _
    SourceType:=xlSourceRange, _
    Filename:=TempFile, _
    Sheet:=TempWB.Sheets(1).Name, _
    Source:=TempWB.Sheets(1).UsedRange.Address, _
    HtmlType:=xlHtmlStatic)
    .Publish (True)
    End With


    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
    "align=left x:publishsource=")


    TempWB.Close savechanges:=False


    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
    End Function
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,610

    Re: Need help in email code

    1.Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution.

    Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.

    To change a Title go to your first post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)


    2. Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, 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

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    02-11-2017
    Location
    Singapore
    MS-Off Ver
    10
    Posts
    98

    Re: Need help in email code

    Hi I am not able to change the contents. Could you please close / delete this post, I will create a a new thread. Will follow the forum rule. Thank you

+ 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. Change VBA to allow email code to bring up outlook email before sending it
    By jonvanwyk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-03-2015, 02:33 AM
  2. [SOLVED] Why is my email code sending a blank email?
    By gmr4evr1 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-28-2015, 10:20 AM
  3. VBA Code to Auto intialsend email & reminder email based on cell value
    By swk8429 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-16-2015, 06:15 AM
  4. VBA code to send email to the people whose email address is in the Access table
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-30-2014, 05:11 AM
  5. Code for email alerts from excel isn't working, wrong code possibly?
    By jessthorogood in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-27-2012, 01:45 AM
  6. Excel code to email whats in the spreadsheet in the body of email
    By Kain93 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-18-2012, 09:55 AM
  7. Replies: 1
    Last Post: 03-08-2012, 01:57 PM

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