Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Excel VBA跳过值为空白的处理_Vba_Excel_Outlook - Fatal编程技术网

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

使用Excel VBA跳过值为空白的处理,vba,excel,outlook,Vba,Excel,Outlook,我有一张Excel表格,上面有电子邮件地址、收件人、抄送、主题等 我有每个附件的文件路径。这些是声明。一些.PDF和一些.XLSX取决于请求。虽然我有每个行的文件路径,但有些行有多列E-L,但并非所有行都有文件路径,并且路径末尾不总是有语句 我需要的VBA代码忽略空白和丢失的文件只附加为发现。这可以是多达9个文件,也可以是一个或一个都没有的文件 在我的测试环境中,我无法让它在没有错误的情况下运行,忽略没有路径的空白单元格或没有文件的路径 Sub SendMail() Dim objOu

我有一张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项目。

假设要忽略的值在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
Sub SendMail()
将对象视为对象
Dim objMail作为对象
将ws设置为工作表
设置objOutlook=CreateObject(“Outlook.Application”)
设置ws=ActiveSheet
对于ws.范围内的每个单元格(“A2:A196”)
如果cell.value为“”,则如果不为空,则执行此操作(您的代码)
设置objMail=objOutlook.CreateItem(0)
用objMail
.To=单元格.Value
.CC=单元格偏移量(0,1).Value
.Subject=cell.Offset(0,2).Value
.Body=单元偏移量(0,3).Value
.Attachments.Add单元格.偏移量(0,4).值
.Attachments.Add单元格.偏移量(0,5).值
.Attachments.Add单元格.偏移量(0,6).值
.Attachments.Add单元格.偏移量(0,7).值
.Attachments.Add单元格.偏移量(0,8).值
.展示
以
Set objMail=Nothing
Else'如果为空,则执行此操作(下一个单元格)
如果结束
下一个细胞
设置ws=Nothing
设置objOutlook=Nothing
端接头

这样试试,当然,您可以随意修改代码以满足您的需要

在表格(“表格1”)中列出以下内容:

宏将循环遍历“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
子发送_文件()
“在Excel 2000-2016中工作
“有关提示,请参阅:http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
Dim OutApp作为对象
将邮件变暗为对象
将sh设置为工作表
暗淡单元格作为范围
将文件单元设置为范围
变暗rng As范围
应用
.EnableEvents=False
.ScreenUpdate=False
以
设置sh=图纸(“图纸1”)
Set-OutApp=CreateObject(“Outlook.Application”)
对于sh.Columns(“B”).Cells.SpecialCells(xlCellTypeConstants)中的每个单元格
'在每行的C:Z列中输入路径/文件名
设置rng=sh.Cells(cell.Row,1).范围(“C1:Z1”)
如果单元格值像“*@*。?*”和_
Application.WorksheetFunction.CountA(rng)>0则
Set-OutMail=OutApp.CreateItem(0)
发邮件
.to=单元格.Value
.Subject=“Testfile”
.Body=“Hi”&单元格偏移量(0,-1).Value
对于rng.SpecialCells中的每个文件单元(xlCellTypeConstants)
如果修剪(文件单元)”,则
如果Dir(FileCell.Value)“,则
.Attachments.Add FileCell.Value
如果结束
如果结束
下一个文件单元
.Send'或use.Display
以
发送邮件=无
如果结束
下一个细胞
设置应用程序=无
应用
.EnableEvents=True
.ScreenUpdate=True
以
端接头

你的问题超出了一个问题的范围。请将您的关注范围缩小到一个问题,以便我们能够集中注意力并回答您的问题。然后对你的下一个问题做一些研究,如果需要的话来这里。循环直到项目完成:)问题已被编辑,现在不清楚要忽略哪些空白。请澄清是否需要忽略A2-A196范围内的空白(如果没有,我将删除此项),谢谢您的时间/协助。我将在测试中尝试。A2-A#.xlsx列E-M中的当前行具有每月保存的文件夹文件的文件路径。其中一些附件多达9个,或者几乎不依赖于该月的.pdf或.xlsx。穿过E-M列A2-A#行的空白单元格。收件人在E-M列中将有多个attach by path,有些收件人只在E列中有一个路径。有些收件人可能当月没有文件,因此忽略“未找到文件”。最终,电子邮件。在指定路径的末尾显示所有指定的附件,并继续为所有(收件人)向下运行宏A2:A196艾尔斯泰
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