【问题标题】:Skip processing where values are blanks using Excel VBA使用 Excel VBA 跳过值为空白的处理
【发布时间】:2018-10-23 21:21:25
【问题描述】:

我有一个包含电子邮件地址、收件人、抄送、主题等的 Excel 表格。

我有每个附件的文件路径。这些是陈述。一些 .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 项目。

【问题讨论】:

  • 您的问题超出了一个问题的范围。请将您的注意力集中在一个问题上,以便我们集中注意力并回答您的问题。然后对你的下一个问题做一些研究,如果需要的话来这里。循环直到项目完成:)

标签: vba excel outlook


【解决方案1】:

试试这种方式,当然可以随意修改代码以满足您的需要..

在 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

【讨论】:

    【解决方案2】:

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

    在这里,ignore 的真正意思是跳到“Else”,您的循环将从这里重新开始。从某种意义上说,它被忽略了,因为 IF 语句告诉它在空白时什么都不做。下一行是“下一个单元格”,它将为您提供所需的结果。

    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
    

    【讨论】:

    • 问题已编辑,现在不清楚您要忽略哪些空白。请说明是否需要忽略 A2 - A196 范围内的空白(如果不需要,我将删除它)
    • 感谢您的时间/协助。将在测试中尝试。 A2-A# 当前行 in.xlsx 列 E-M 具有每月保存的文件夹文件的文件路径。当月有多达 9 个附件或少至不依赖 .pdf 或 .xlsx。跨列 E-M 下线 A2-A# 的空白单元格。收件人将在 Col E-M 中按路径进行多次附加,有些人只会在 E 列中有一个路径。有些人当月可能没有文件,所以忽略“找不到文件”。最后,电子邮件 .display 将在指定路径的末尾显示所有指定的附件,并继续为所有(收件人)在 A2:A196 下运行宏。 “其他”类型
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 2016-05-27
    • 1970-01-01
    相关资源
    最近更新 更多