如何通过VBA

时间:2017-03-16 08:42:20

标签: excel vba excel-vba outlook outlook-vba

我编写了一个代码,它将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

1 个答案:

答案 0 :(得分:0)

只需重复使用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