3

Hi I have used the Ron De Bruin's fantastic website to create VBA code that generates an email to specic users from an excel file.

The only thing is that my signature does not appear on each email and I cannot seem to find how to add it within the code?

Would anyone be able to advise please?

As you can tell I am a complete novice!

Module 1

Option Explicit Sub Send_Row_Or_Rows_2()

Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim Ash As Worksheet
Dim Cws As Worksheet
Dim Rcount As Long
Dim Rnum As Long
Dim FilterRange As Range
Dim FieldNum As Integer
Dim strbody As String

On Error GoTo cleanup
Set OutApp = CreateObject("Outlook.Application")

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

'Set filter sheet, you can also use Sheets("MySheet")
Set Ash = ActiveSheet

 strbody = "<BODY style=font-size:11pt;font-family:Calibri>Hi;<p>Please see below details of outstanding files.  We will require these by 25th December 2017.  Please feel free to respond with any questions.<p>Thank you.</BODY>"

'Set filter range and filter column (column with e-mail addresses)
Set FilterRange = Ash.Range("A1:L" & Ash.Rows.Count)
FieldNum = 2    'Filter column = B because the filter range start in column A

'Add a worksheet for the unique list and copy the unique list in A1
Set Cws = Worksheets.Add
FilterRange.Columns(FieldNum).AdvancedFilter _
        Action:=xlFilterCopy, _
        CopyToRange:=Cws.Range("A1"), _
        CriteriaRange:="", Unique:=True

'Count of the unique values + the header cell
Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))

'If there are unique values start the loop
If Rcount >= 2 Then
    For Rnum = 2 To Rcount

        'Filter the FilterRange on the FieldNum column
        FilterRange.AutoFilter Field:=FieldNum, _
                               Criteria1:=Cws.Cells(Rnum, 1).Value

        'If the unique value is a mail addres create a mail
        If Cws.Cells(Rnum, 1).Value Like "?*@?*.?*" Then

            With Ash.AutoFilter.Range
                On Error Resume Next
                Set rng = .SpecialCells(xlCellTypeVisible)
                On Error GoTo 0
            End With

            Set OutMail = OutApp.CreateItem(0)

            On Error Resume Next
            With OutMail
                .to = Cws.Cells(Rnum, 1).Value
                .Subject = "Test mail"
                .HTMLBody = strbody & RangetoHTML(rng)
                .Display  'Or use Send
            End With
            On Error GoTo 0

            Set OutMail = Nothing
        End If

        'Close AutoFilter
        Ash.AutoFilterMode = False

    Next Rnum
End If

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

Module 2:

Option Explicit

Function RangetoHTML(rng As Range)

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

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
BruceWayne
  • 22,923
  • 15
  • 65
  • 110
withnoice
  • 31
  • 1
  • There is no way to make your signature appear when creating automated emails using code. – braX Dec 12 '17 at 14:30
  • Are you trying to dynamically create a signature? What are you trying to accomplish with "TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm""? – Xabier Dec 12 '17 at 14:35
  • @braX if your signature is saved as a .htm and it is saved somewhere on your computer (as long as src for images are absolute), then it is possible as I've done so myself. – Xabier Dec 12 '17 at 14:36
  • True, you can simulate the signature, and add it in, but not the actual signature itself. – braX Dec 12 '17 at 14:38
  • If you create a template `.oft` file, you can use your default signature for it. It's my alternative to it – Maldred Dec 12 '17 at 15:19
  • I have managed to locate my signature .htm file @Xabier is there an easy way to embed this into emails I am generating with the same formatting? Thanks for you responses so far – withnoice Dec 13 '17 at 19:31

3 Answers3

1

Translate your signature to a HTML string and add it to the email. Like this:

Dim mySignature As String
mySignature = "<p>Best Regards,<p>Your name and company<p>"

With OutMail
    .to = Cws.Cells(Rnum, 1).Value
    .Subject = "Test mail"
    .HTMLBody = strbody & RangetoHTML(Rng) & mySignature
    .Display  'Or use Send
End With
Vityata
  • 42,633
  • 8
  • 55
  • 100
0

Please give this a try to see if your issue gets resolved...

With OutMail
    .BodyFormat = 2
    .Display = True
    .To = Cws.Cells(Rnum, 1).Value
    .Subject = "Test mail"
    .HTMLBody = strbody & RangetoHTML(rng) & "<br>" & .HTMLBody
    '.Send 'To send
End With
Subodh Tiwari sktneer
  • 9,906
  • 2
  • 18
  • 22
0

If you were to replace everything in Module 1 with the following, I'm sure it should work, just remember to replace the name of your .htm signature file and edit that htm to include all image sources as absolute:

Option Explicit

Sub Send_Row_Or_Rows_2()
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim Ash As Worksheet
Dim Cws As Worksheet
Dim Rcount As Long
Dim Rnum As Long
Dim FilterRange As Range
Dim FieldNum As Integer
Dim strbody As String
Dim SigString As String
Dim Signature As Variant

On Error GoTo cleanup
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

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

'Set filter sheet, you can also use Sheets("MySheet")
Set Ash = ActiveSheet

strbody = "<BODY style=font-size:11pt;font-family:Calibri>Hi;<p>Please see below details of outstanding files.  We will require these by 25th December 2017.  Please feel free to respond with any questions.</p>Thank you.</BODY>"

SigString = Environ("appdata") & _
    "\Microsoft\Signatures\YourSignature.htm"

'CHANGE ABOVE TO YOUR SIGNATURE NAME .htm
'Make sure that the Htm file has all sources defined with absolute references
'so if an image's src=\img\signature.jpg, then you should change \img\signature to something like:
'C:\Users\Me\AppData\Roaming\Microsoft\Signatures\

If Dir(SigString) <> "" Then
    Signature = GetBoiler(SigString)
Else
    Signature = ""
End If

'Set filter range and filter column (column with e-mail addresses)
Set FilterRange = Ash.Range("A1:L" & Ash.Rows.Count)
FieldNum = 2    'Filter column = B because the filter range start in column A

'Add a worksheet for the unique list and copy the unique list in A1
Set Cws = Worksheets.Add
FilterRange.Columns(FieldNum).AdvancedFilter _
        Action:=xlFilterCopy, _
        CopyToRange:=Cws.Range("A1"), _
        CriteriaRange:="", Unique:=True

'Count of the unique values + the header cell
Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))

'If there are unique values start the loop
If Rcount >= 2 Then
    For Rnum = 2 To Rcount

        'Filter the FilterRange on the FieldNum column
        FilterRange.AutoFilter Field:=FieldNum, _
                               Criteria1:=Cws.Cells(Rnum, 1).Value

        'If the unique value is a mail addres create a mail
        If Cws.Cells(Rnum, 1).Value Like "?*@?*.?*" Then

            With Ash.AutoFilter.Range
                On Error Resume Next
                Set rng = .SpecialCells(xlCellTypeVisible)
                On Error GoTo 0
            End With

            Set OutMail = OutApp.CreateItem(0)

            On Error Resume Next
            With OutMail
                .to = Cws.Cells(Rnum, 1).Value
                .Subject = "Test mail"
                .HTMLBody = strbody & RangetoHTML(rng) & "<br>" & Signature
                .Display  'Or use Send
            End With
            On Error GoTo 0

            Set OutMail = Nothing
        End If

        'Close AutoFilter
        Ash.AutoFilterMode = False

    Next Rnum
End If

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

Function GetBoiler(ByVal sFile As String) As String
'https://www.rondebruin.nl/win/s1/outlook/signature.htm
    Dim fso As Object
    Dim ts As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
    GetBoiler = ts.readall
    ts.Close
End Function
Xabier
  • 7,587
  • 1
  • 8
  • 20