【问题标题】:VB.NET error "Expression is of type 'Attachments', which is not a collection type"VB.NET 错误\“表达式的类型为\'附件\',它不是集合类型\”
【发布时间】:2023-01-07 02:20:14
【问题描述】:

我想从 outlook .msg 文件中读取附件。 我从此处找到的另一个解决方案复制了 VB 代码,但在“对于 msg.Attachments 中的每个 att”循环中,我收到错误“表达式的类型为‘附件’,这不是集合类型”。

我正在使用 Visual Studio 2019。 你能帮我吗?

Sub SaveAttachments()
    Dim msg As Outlook.MailItem
    Dim att As Outlook.Attachment
    Dim strFilePath As String
    Dim strAttPath As String
    Dim strFile As String

    strFilePath = "C:\Users\...\Desktop\Test\"
    strAttPath = "C:\Users\...\extracted attachment\"
    strFile = Dir(strFilePath & "<Doc Name>.msg")

    Do While Len(strFile) > 0
        msg = Application.CreateItemFromTemplate(strFilePath & strFile)
        If msg.Attachments.Count > 0 Then
            For Each att In msg.Attachments
                att.SaveAsFile(strAttPath & att.FileName)
            Next
        End If
        strFile = Dir()
    Loop
End Sub

【问题讨论】:

    标签: vb.net types foreach collections expression


    【解决方案1】:

    Outlook 类型是通过 COM Interop 提供的。他们早于 .Net 并且对提供 .Net 的 GetEnumerator() 方法一无所知,这就是 For Each 循环的工作原理。您将需要按索引手动循环。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多