Excel 如何在outlook中通过VBA在表格后添加正文

Excel 如何在outlook中通过VBA在表格后添加正文,excel,vba,outlook,Excel,Vba,Outlook,我已经编写了一个代码,从excel复制范围,并将其粘贴到outlook中作为一个表 我想在表格后面添加正文,例如: “上表只是一个表。我在表前添加了文本,但我想在表后添加正文 代码如下: Sub Send_Row_Or_Rows_1() 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

我已经编写了一个代码,从excel复制范围,并将其粘贴到outlook中作为一个表

我想在表格后面添加正文,例如:

“上表只是一个表。我在表前添加了文本,但我想在表后添加正文

代码如下:

Sub Send_Row_Or_Rows_1()

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 mailAddress As String
Dim StrBody As String

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

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


Set Ash = ActiveSheet

'Set filter range and filter column (Column with names)
Set FilterRange = Ash.Range("A1:H" & Ash.Rows.Count)
FieldNum = 1    'Filter column = A because the filter range start in 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

        'Look for the mail address in the MailInfo worksheet
        mailAddress = ""
        On Error Resume Next
        mailAddress = Application.WorksheetFunction. _
                      VLookup(Cws.Cells(Rnum, 1).Value, _
                            Worksheets("Mailinfo").Range("A1:B" & _
                            Worksheets("Mailinfo").Rows.Count), 2, False)
        On Error GoTo 0

        If mailAddress <> "" Then
            With Ash.AutoFilter.Range
                On Error Resume Next
                Set rng = Application.Intersect(.SpecialCells(xlCellTypeVisible), Ash.Range("B:H"))
                On Error GoTo 0
            End With

            Set OutMail = OutApp.CreateItem(0)

            On Error Resume Next

            With OutMail
                .to = mailAddress

                .Subject = "Test mail"
                .HTMLBody = StrBody & RangetoHTML(rng)
                .Display  'Or use Send

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

            End With
            On Error GoTo 0


            Set OutMail = Nothing
        End If

        'Close AutoFilter
        Ash.AutoFilterMode = False

    Next Rnum
End If

cleanup:
Set OutApp = Nothing
Application.DisplayAlerts = False
Cws.Delete
Application.DisplayAlerts = True

With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With
End Sub
Sub Send_Row_或_Rows_1()
Dim OutApp作为对象
将邮件变暗为对象
变暗rng As范围
将灰烬作为工作表
将Cws设置为工作表
暗计数等于长
暗淡如长
模糊过滤范围
Dim FieldNum作为整数
将邮件地址设置为字符串
像弦一样暗的链子
关于错误转到清理
Set-OutApp=CreateObject(“Outlook.Application”)
应用
.EnableEvents=False
.ScreenUpdate=False
以
设置Ash=ActiveSheet
'设置筛选器范围和筛选器列(带名称的列)
设置FilterRange=Ash.Range(“A1:H”和Ash.Rows.Count)
FieldNum=1'筛选列=A,因为筛选范围从A开始
'为唯一列表添加工作表,并在A1中复制唯一列表
设置Cws=工作表。添加
FilterRange.Columns(FieldNum).AdvancedFilter_
操作:=xlFilterCopy_
CopyToRange:=Cws.Range(“A1”)_
CriteriaRange:=“”,唯一:=真
'唯一值的计数+标题单元格
Rcount=Application.WorksheetFunction.CountA(Cws.Columns(1))
'如果存在唯一值,则启动循环
如果Rcount>=2,则
对于Rnum=2,则为Rcount
'筛选FieldNum列上的FilterRange
FilterRange.AutoFilter字段:=FieldNum_
准则1:=Cws.Cells(Rnum,1).Value
'在MailInfo工作表中查找邮件地址
mailAddress=“”
出错时继续下一步
mailAddress=Application.WorksheetFunction_
VLookup(Cws.单元格(Rnum,1).值_
工作表(“邮件信息”).范围(“A1:B”和_
工作表(“邮件信息”).Rows.Count),2,False)
错误转到0
如果邮件地址为“”,则
使用Ash.AutoFilter.Range
出错时继续下一步
Set rng=Application.Intersect(.SpecialCells(xlCellTypeVisible),Ash.Range(“B:H”))
错误转到0
以
Set-OutMail=OutApp.CreateItem(0)
出错时继续下一步
发邮件
.to=邮寄地址
.Subject=“测试邮件”
.HTMLBody=StrBody和RangetoHTML(rng)
.显示或使用“发送”
床身=床单(“床身”).范围(“A1”).值&“
”&“
”&”_ 板材(“主体”)。范围(“A2”)。值和_ 板材(“主体”).范围(“A3”).值和“


” 以 错误转到0 发送邮件=无 如果结束 '关闭自动过滤器 Ash.AutoFilterMode=False 下一个Rnum 如果结束 清理: 设置应用程序=无 Application.DisplayAlerts=False 删除 Application.DisplayAlerts=True 应用 .EnableEvents=True .ScreenUpdate=True 以 端接头
只需重复使用您已经放入HTMLBody中的内容:

.HTMLBody = .HTMLBody  & "The above table is just a table."
完整更正代码:

Sub Send_Row_Or_Rows_1()

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 mailAddress As String
Dim StrBody As String

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

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


Set Ash = ActiveSheet

'Set filter range and filter column (Column with names)
Set FilterRange = Ash.Range("A1:H" & Ash.Rows.Count)
FieldNum = 1    'Filter column = A because the filter range start in 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

        'Look for the mail address in the MailInfo worksheet
        mailAddress = ""
        On Error Resume Next
        mailAddress = Application.WorksheetFunction. _
                      VLookup(Cws.Cells(Rnum, 1).Value, _
                            Worksheets("Mailinfo").Range("A1:B" & _
                            Worksheets("Mailinfo").Rows.Count), 2, False)
        On Error GoTo 0

        If mailAddress <> "" Then
            With Ash.AutoFilter.Range
                On Error Resume Next
                Set rng = Application.Intersect(.SpecialCells(xlCellTypeVisible), Ash.Range("B:H"))
                On Error GoTo 0
            End With

            Set OutMail = OutApp.CreateItem(0)

            On Error Resume Next

            With OutMail
                .to = mailAddress

                .Subject = "Test mail"
                StrBody = Sheets("Body").Range("A1").Value & "<br>" & "<br>" & _
          Sheets("Body").Range("A2").Value & "<br>" & "<br>" & _
          Sheets("Body").Range("A3").Value & "<br><br><br>"
                .HTMLBody = StrBody & RangetoHTML(rng)

                ''Just reuse what you already put in the HTMLBody :
                .HTMLBody = .HTMLBody  & "The above table is just a table. I have added text before the table but I want to add body after the table."




                .Display  'Or use Send
            End With
            On Error GoTo 0


            Set OutMail = Nothing
        End If

        'Close AutoFilter
        Ash.AutoFilterMode = False

    Next Rnum
End If

cleanup:
Set OutApp = Nothing
Application.DisplayAlerts = False
Cws.Delete
Application.DisplayAlerts = True

With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With
End Sub
Sub Send_Row_或_Rows_1()
Dim OutApp作为对象
将邮件变暗为对象
变暗rng As范围
将灰烬作为工作表
将Cws设置为工作表
暗计数等于长
暗淡如长
模糊过滤范围
Dim FieldNum作为整数
将邮件地址设置为字符串
像弦一样暗的链子
关于错误转到清理
Set-OutApp=CreateObject(“Outlook.Application”)
应用
.EnableEvents=False
.ScreenUpdate=False
以
设置Ash=ActiveSheet
'设置筛选器范围和筛选器列(带名称的列)
设置FilterRange=Ash.Range(“A1:H”和Ash.Rows.Count)
FieldNum=1'筛选列=A,因为筛选范围从A开始
'为唯一列表添加工作表,并在A1中复制唯一列表
设置Cws=工作表。添加
FilterRange.Columns(FieldNum).AdvancedFilter_
操作:=xlFilterCopy_
CopyToRange:=Cws.Range(“A1”)_
CriteriaRange:=“”,唯一:=真
'唯一值的计数+标题单元格
Rcount=Application.WorksheetFunction.CountA(Cws.Columns(1))
'如果存在唯一值,则启动循环
如果Rcount>=2,则
对于Rnum=2,则为Rcount
'筛选FieldNum列上的FilterRange
FilterRange.AutoFilter字段:=FieldNum_
准则1:=Cws.Cells(Rnum,1).Value
'在MailInfo工作表中查找邮件地址
mailAddress=“”
出错时继续下一步
mailAddress=Application.WorksheetFunction_
VLookup(Cws.单元格(Rnum,1).值_
工作表(“邮件信息”).范围(“A1:B”和_
工作表(“邮件信息”).Rows.Count),2,False)
错误转到0
如果邮件地址为“”,则
使用Ash.AutoFilter.Range
出错时继续下一步
Set rng=Application.Intersect(.SpecialCells(xlCellTypeVisible),Ash.Range(“B:H”))
错误转到0
以
Set-OutMail=OutApp.CreateItem(0)
出错时继续下一步
发邮件
.to=邮寄地址
.Subject=“测试邮件”
床身=床单(“床身”).范围(“A1”).值&“
”&“
”&”_ 板材(“主体”)。范围(“A2”)。值和_ 板材(“主体”).范围(“A3”).值和“


” .HTMLBody=StrBody和RangetoHTML(rng) ''只需重复使用您已经放入HTMLBody中的内容: .HTMLBody=.HTMLBody&“上表只是一个表。我在表格前添加了文本,但我想在表格后添加正文。” .显示或使用“发送” 以 错误转到0 发送邮件=无 如果结束 '关闭自动过滤器 Ash.AutoFilterMode=False 下一个Rnum 如果结束 清理: 设置应用程序=无 应用程序