在 outlook 中报告电子邮件的附件

Report on attachments of emails in outlook

我基本上是在尝试为我的更大规模的宏创建一个报告。该报告将显示已处理的每封邮件以及在电子邮件中找到的附件。

我可以使用以下代码,但无法仅向我提供 .csv 文件的正确结果。谁能看到我看不到的问题?

    If .Attachments.Count = 0 Then
    csv_report = "NO"
    pdf_report = "NO"
    xls_report = "NO"
    End If

    If .Attachments.Count > 0 Then
    For i2 = 1 To .Attachments.Count
    If LCase(Right(.Attachments(i2).Filename, 4)) = ".csv" Then
    csv_report = "YES"
    Else
    csv_report = "NO"
    End If
    If LCase(Right(.Attachments(i2).Filename, 4)) = ".pdf" Then
    pdf_report = "YES"
    Else
    pdf_report = "NO"
    End If
    If LCase(Right(.Attachments(i2).Filename, 4)) = ".xls" Or LCase(Right(.Attachments(i2).Filename, 5)) = ".xlsx" Then
    xls_report = "YES"
    Else
    xls_report = "NO"
    End If
    Next
    End If

    Sheets("Mail Report").Activate
    Range("C65000").End(xlUp).Offset(1).Value = csv_report
    Range("D65000").End(xlUp).Offset(1).Value = pdf_report
    Range("E65000").End(xlUp).Offset(1).Value = xls_report

    subject_line = mail.Subject
    Range("A65000").End(xlUp).Offset(1).Value = subject_line

因此,只需添加 'GoTo' 函数,我就能够回答我自己的查询。谢谢大家留下评论

    If .Attachments.Count = 0 Then
    csv_report = "NO"
    pdf_report = "NO"
    xls_report = "NO"
    End If

    If .Attachments.Count > 0 Then
    For i2 = 1 To .Attachments.Count
    If LCase(Right(.Attachments(i2).Filename, 4)) = ".csv" Then
    csv_report = "YES"
    GoTo CSVyes        'if a .csv file is found, it skips to the PDF attachment checker
    Else
    csv_report = "NO"
    End If
    Next

CSVyes:
    For i2 = 1 To .Attachments.Count
    If LCase(Right(.Attachments(i2).Filename, 4)) = ".pdf" Then
    pdf_report = "YES"
    GoTo PDFyes        'if a .pdf file is found, it skips to the XLS attachment checker
    Else
    pdf_report = "NO"
    End If
    Next

PDFyes:
    For i2 = 1 To .Attachments.Count
    If LCase(Right(.Attachments(i2).Filename, 4)) = ".xls" Or LCase(Right(.Attachments(i2).Filename, 5)) = ".xlsx" Or UCase(Right(.Attachments(i2).Filename, 4)) = ".XLS" Then
    xls_report = "YES"
    GoTo XLSyes        'if a .xls file is found, it skips to the end of the checks
    Else
    xls_report = "NO"
    End If
    Next

XLSyes:
    End If

    Sheets("Mail Report").Activate
    Range("C65000").End(xlUp).Offset(1).Value = csv_report
    Range("D65000").End(xlUp).Offset(1).Value = pdf_report
    Range("E65000").End(xlUp).Offset(1).Value = xls_report

    subject_line = mail.Subject
    Range("A65000").End(xlUp).Offset(1).Value = subject_line