vba - 从 excel/vba 生成电子邮件到 Outlook 时,我的电子邮件签名不会出现?

标签 vba excel email outlook

您好,我使用 Ron De Bruin 的精彩网站创建了 VBA 代码,该代码可以从 Excel 文件生成发送给特定用户的电子邮件。

唯一的问题是我的签名没有出现在每封电子邮件上,而且我似乎找不到如何在代码中添加它?

请问有人可以给点建议吗?

如你所知,我是一个十足的新手!

模块 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

模块 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

最佳答案

将您的签名转换为 HTML 字符串并将其添加到电子邮件中。像这样:

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

关于vba - 从 excel/vba 生成电子邮件到 Outlook 时,我的电子邮件签名不会出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47774472/

相关文章:

excel - 自定义公式太慢

excel - 使用 VBA 根据另一个单元格值设置 Excel 单元格值

excel - 通过 VBA 创建数据透视表时出错

excel - 具有动态范围和 customOrder excel 的排序宏

css - 编码宽度显示和报告不正确

excel - 使用 VBA 更改图表中的数据范围

Excel 不等式查找比较

vba - 通过 Excel VBA 的 HTTP Post 协助

php - 如何在更新 MYSQL 数据库时向列表发送电子邮件

PHP 嵌入图像未在 Web 浏览器上显示