【问题标题】:Report on attachments of emails in outlook在 Outlook 中报告电子邮件的附件
【发布时间】:2015-05-06 12:18:19
【问题描述】:

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

我可以使用以下代码,但没有为我提供仅适用于 .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

【问题讨论】:

  • 我看到的最大问题是你没有使用For Each 循环。
  • 您是否尝试调试代码并查看 FileName 实际值?
  • 如何构建“For Each”循环?我会尝试使用 msgBox 来调试代码
  • Dim Att as AttachmentFor Each Att in .AttachmentsNext Att 在循环结束! ;) 并在循环中Att.FileName
  • 你应该加入你的 If .Attachments.Count = 0 ThenIf .Attachments.Count > 0 Then 到:如果 .Attachments.Count <= 0 ThenElse

标签: excel vba email outlook email-attachments


【解决方案1】:

因此,只需添加“GoTo”功能,我就能回答我自己的查询。谢谢大家离开cmets

    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

【讨论】:

    猜你喜欢
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    相关资源
    最近更新 更多