使用Excel VBA跳过值为空白的处理

时间:2018-06-01 18:52:31

标签: excel vba excel-vba outlook

我有一张带有电子邮件地址的Excel表格,To,CC,Subject等。

我有每个附件的文件路径。这些是陈述。一些.PDF和一些.XLSX取决于请求。虽然我有每个文件路径,但有些列有多个列E-L,但并非所有行都有文件路径,并且不总是路径末尾的语句。

我需要VBA代码来忽略只有附加的空白和丢失的文件。这可以是多达9个文件,或者只有一个或者没有按行到收件人。

我无法在没有错误的情况下运行它,在我的测试环境中,忽略没有路径的空白单元格或没有文件的路径。

Sub SendMail()

    Dim objOutlook As Object
    Dim objMail As Object
    Dim ws As Worksheet

    Set objOutlook = CreateObject("Outlook.Application")
    Set ws = ActiveSheet

    For Each cell In ws.Range("A2:A196")

        Set objMail = objOutlook.CreateItem(0)

        With objMail
            .To = cell.Value
            .CC = cell.Offset(0, 1).Value
            .Subject = cell.Offset(0, 2).Value
            .Body = cell.Offset(0, 3).Value
            .Attachments.Add cell.Offset(0, 4).Value
            .Attachments.Add cell.Offset(0, 5).Value
            .Attachments.Add cell.Offset(0, 6).Value
            .Attachments.Add cell.Offset(0, 7).Value
            .Attachments.Add cell.Offset(0, 8).Value
            .Display
        End With

        Set objMail = Nothing
    Next cell

    Set ws = Nothing
    Set objOutlook = Nothing

End Sub

这是我的第一个VBA项目。

2 个答案:

答案 0 :(得分:0)

这将忽略您范围内的空白单元格,假设您要忽略的值位于范围A2 - A196中。

在这里,忽略真的意味着跳到你的循环将重新开始的“Else”。从IF语句告诉它在空白时什么都不做的意义上来说它被忽略了。下一行是“Next Cell”,它将为您提供所需的结果。

Sub SendMail()

Dim objOutlook As Object
Dim objMail As Object
Dim ws As Worksheet

Set objOutlook = CreateObject("Outlook.Application")
Set ws = ActiveSheet

For Each cell In ws.Range("A2:A196")
If cell.value <> "" Then  'If NOT blank, do this (your code)
Set objMail = objOutlook.CreateItem(0)

    With objMail
        .To = cell.Value
      .CC = cell.Offset(0, 1).Value
        .Subject = cell.Offset(0, 2).Value
        .Body = cell.Offset(0, 3).Value
        .Attachments.Add cell.Offset(0, 4).Value
        .Attachments.Add cell.Offset(0, 5).Value
        .Attachments.Add cell.Offset(0, 6).Value
        .Attachments.Add cell.Offset(0, 7).Value
        .Attachments.Add cell.Offset(0, 8).Value
        .Display
    End With

    Set objMail = Nothing
Else 'If IS blank, do this (next cell)
End If 
Next cell

Set ws = Nothing
Set objOutlook = Nothing

End Sub

答案 1 :(得分:0)

以这种方式尝试,当然可以随意修改代码以适合您的需求。

使用:在Sheets(“ Sheet1”)中创建列表:

In column A : Names of the people
In column B : E-mail addresses
In column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files)

宏将循环遍历“ Sheet1”中的每一行,并且如果B列中有电子邮件地址 和C:Z列中的文件名,它将创建包含此信息的邮件并发送。

Sub Send_Files()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim cell As Range
    Dim FileCell As Range
    Dim rng As Range

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

    Set sh = Sheets("Sheet1")

    Set OutApp = CreateObject("Outlook.Application")

    For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)

        'Enter the path/file names in the C:Z column in each row
        Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")

        If cell.Value Like "?*@?*.?*" And _
           Application.WorksheetFunction.CountA(rng) > 0 Then
            Set OutMail = OutApp.CreateItem(0)

            With OutMail
                .to = cell.Value
                .Subject = "Testfile"
                .Body = "Hi " & cell.Offset(0, -1).Value

                For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                    If Trim(FileCell) <> "" Then
                        If Dir(FileCell.Value) <> "" Then
                            .Attachments.Add FileCell.Value
                        End If
                    End If
                Next FileCell

                .Send  'Or use .Display
            End With

            Set OutMail = Nothing
        End If
    Next cell

    Set OutApp = Nothing
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub

https://www.rondebruin.nl/win/s1/outlook/amail6.htm