简体   繁体   English

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

[英]Hide a column to be sent via mail excel vba

I have the following code below which divides every row in a cell based on email address and then sends mail to that email address. 我下面有以下代码,该代码根据电子邮件地址划分单元格中的每一行,然后将邮件发送到该电子邮件地址。 However, I am looking to hide the email address when sending the mail (which is column K). 但是,我希望在发送邮件时隐藏电子邮件地址(即K列)。 I tried to use copyrange function but din't work, can someone help me out with this doubt please? 我试图使用copyrange函数,但没有用,有人可以帮我解决这个疑问吗?

在此处输入图片说明 Code: 码:

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

I hope this is the code you required. 我希望这是您所需的代码。 It will copy the range except the last column (email column) and paste into email. 它将复制除最后一列(电子邮件列)以外的范围并将其粘贴到电子邮件中。

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

the above code does the trick.below is the full code 上面的代码可以解决问题,下面是完整的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Outlook 间歇性地不使用 Excel 发送邮件 VBA - Outlook mail intermittently not sent using Excel VBA 通过VBA隐藏Excel工作表 - Hide excel worksheet via VBA Outlook 2010规则是否对从Excel VBA发送的邮件运行? - Do Outlook 2010 rules run on mail sent from Excel VBA? Excel vba 发Outlook 邮箱:.发function 不发function - Excel vba to sent Outlook mail: .Send function does not function 通过vba在excel中打开邮件模板,并用excel数据填充模板 - Open mail template in excel via vba and fill template with excel data 通过Excel VBA作为电子邮件附件发送的文件始终损坏 - File sent as email attachment via Excel VBA is always corrupt 发送第一封电子邮件后,自动发送第二封电子邮件(Excel-VBA代码) - Send a second E-Mail automatically once the first E-Mail is sent (Excel-VBA-Code) 在VBA Excel中通过CDO提取Gmail收件箱邮件 - Fetching gmail inbox mail messages via CDO in vba excel VBA Outlook 邮件正文不显示(可能是由于表格通过 vba excel 粘贴到邮件正文中) - VBA Outlook mail body does not display (probably due to table pasted into mail body via vba excel) Outlook VBA从邮件中保存附件,然后将附件数据复制到另一个excel中并通过邮件发送发送excel - Outlook VBA to save attachment from a mail,and then copy the attachment data in another excel and send the send excel via mail
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM