代码之家  ›  专栏  ›  技术社区  ›  Shivang

隐藏要通过邮件excel vba发送的列

  •  0
  • Shivang  · 技术社区  · 6 年前

    我有下面的代码,它根据电子邮件地址划分单元格中的每一行,然后将邮件发送到该电子邮件地址。但是,我希望在发送邮件时隐藏电子邮件地址(列K)。我试过使用copyrange功能,但不起作用,有人能帮我解决这个疑问吗?

    enter image description here 代码:

    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
    
    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
    
    'Set filter range and filter column (column with e-mail addresses)
    Set FilterRange = Ash.Range("A1:K" & Ash.Rows.Count)
    FieldNum = 11    'Filter column = K 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 = 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
    
    cleanup:
    Set OutApp = Nothing
    Application.DisplayAlerts = False
    Cws.Delete
    Application.DisplayAlerts = True
    
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    End Sub
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Durgaprasad    6 年前

    我希望这是你需要的代码。它将复制除最后一列(email列)之外的范围并粘贴到email中。

    With .Resize(.Rows.Count, .Columns.Count - 1)
    Set rng = .SpecialCells(xlCellTypeVisible)
    End With
    

    上面的代码起到了作用

    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
    
    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
    
    'Set filter range and filter column (column with e-mail addresses)
    Set FilterRange = Ash.Range("A1:K" & Ash.Rows.Count)
    FieldNum = 11    'Filter column = K 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
                    With .Resize(.Rows.Count, .Columns.Count - 1)
                        Set rng = .SpecialCells(xlCellTypeVisible)
                    End With
    
                    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 = 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
    
    cleanup:
    Set OutApp = Nothing
    Application.DisplayAlerts = False
    Cws.Delete
    Application.DisplayAlerts = True
    
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    End Sub