【问题标题】:Email attachments recorded in ExcelExcel 中记录的电子邮件附件
【发布时间】:2015-05-12 10:43:52
【问题描述】:

下面的宏旨在接收 x 数量的电子邮件,计算每封邮件中有多少附件,然后定位某些文件格式。然后它将记录在某个 Excel 电子表格中找到的内容。

宏运行良好,但我现在想在另一个场景中添加。我要添加的场景是,如果一封电子邮件有超过 1 个 .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"
        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

【问题讨论】:

  • 对不起,如果我没有说清楚。但这会导致包含 1 个 .csv 文件的电子邮件显示为“多个”。我希望它在 1 时保持为“是”,但在超过 1 时保持为“多个”
  • 在下面查看我的答案。

标签: email excel outlook email-attachments vba


【解决方案1】:

检查下面的代码。我刚刚添加了If Else 块来检查 Attachment.count > 1。就是这样。

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

【讨论】:

  • 那行不通。这是过程: 选择电子邮件 - 如果附件计数大于 - 选择第一个附件 - 如果 .csv 文件 - “是”(转到 PDF) - 如果不是 .csv 文件 - “否”(转到 PDF) - 重复使用相同附件检查 PDF 和 XLS 的过程。
  • 我对这一行感到困惑 - “如果 .csv 文件 - “是”(转到 PDF) - 如果不是 .csv 文件 - “否”(转到 PDF)”。请澄清
【解决方案2】:

在多次尝试实施此方案后,我没有成功。所以我想在这个宏之后放一个替代方法。

这个宏只是更大规模宏的一小部分,但基本上当 B 列有空白时,我会将“是”替换为“多个”,这对结果来说已经足够了。

Dim kk As Long

Sheets("Mail Report").Activate
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For kk = 1 To LastRow
If IsEmpty(Cells(kk, 2)) Then
    Cells(kk, 2) = MAIN_PATH & "For Resolution\" & "multiple_csv\"
    Cells(kk, 3) = "MULTIPLE"
End If
Next kk

【讨论】:

    猜你喜欢
    • 2012-02-22
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 2018-12-30
    • 2021-11-24
    • 2019-09-25
    相关资源
    最近更新 更多