+ Reply to Thread
Results 1 to 2 of 2

Add text and signature to email script

  1. #1
    Registered User
    Join Date
    11-30-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    1

    Add text and signature to email script

    Hi I have an email script i use every week to send a few cells of data to different clients from an excel spreadsheet. the script i use converts the range of cells to html opens an email and pastes the data in. I would like to add text in to the body of the email as well saying " Hi Please find the weekly hours below". I also need the script to keep my corporate email signature.
    I have attached the script below. I have visited many forums and have seen that this can be done but i have strugled to make it function. My working script is below, if someone could asses the script and add in the relevant information i would be so happy.


    Sub NGARDA_WEEKLY_HOURS()

    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object

    Set rng = Nothing
    On Error Resume Next
    Set rng = Sheets("Hire EOM Hours").Range("G8:H18").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If rng Is Nothing Then
    MsgBox "The selection is not a range or the sheet is protected" & _
    vbNewLine & "please correct and try again.", vbOKOnly
    Exit Sub
    End If

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

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

    On Error Resume Next
    With OutMail
    .To = Sheets("Hire EOM Hours").Range("T16")
    .CC = Sheets("Hire EOM Hours").Range("T17")
    .BCC = ""
    .Subject = Sheets("Hire EOM Hours").Range("T1")
    .HTMLBody = RangetoHTML(rng)
    .Display
    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"

    'Copy the range and create a new workbook to past the data in
    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

    'Publish the sheet to a htm file
    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

    'Read all data from the htm file into RangetoHTML
    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=")

    'Close TempWB
    TempWB.Close savechanges:=False

    'Delete the htm file we used in this function
    Kill TempFile

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

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Add text and signature to email script

    Hi, Clcheney,

    with such long codes yoiu should wrap them up with code-tags for a better readability as requested per Forum Rule #3.

    Maybe have a look at Insert Outlook Signature in mail.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for 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. send selected range in email with default outlook email signature included
    By mdsickler in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-04-2013, 10:50 PM
  2. Mail Signature Script Error
    By tigerdel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-20-2012, 09:19 PM
  3. Unable to link default email signature to email
    By paxile2k in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-15-2010, 06:52 PM
  4. Email using Excel VBA and Keeping Outlook Signature on the Email
    By Shama in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-13-2009, 07:39 PM
  5. [SOLVED] Email Body Text & Signature
    By Darrell Lankford in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-07-2006, 12:55 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