首页 > 解决方案 > VB.net - 从共享文件夹中读取 .msg 文件并提取其中的附件

问题描述

我对 VB 完全陌生,我正在尝试使用以下代码提取保存在 .msg 文件中的附件。如果这是正确的方法,有人可以帮助我吗?

我面临以下编译器错误。有人可以帮我解决这个问题吗?

  1. Outlook.Attachment 未定义。
  2. End Sub'必须以匹配的'Sub'开头
  3. 对非共享成员的引用需要对象引用。
  4. 语句不能出现在方法主体中。假定方法结束
  5. 方法参数必须用括号括起来。
  6. 未定义类型“Outlook.MailItem”。

Sub SaveOlAttachments()
    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.netoutlook

解决方案


CreateItemFromTemplate您可以使用Namespace.OpenSharedItem打开 MSG 文件,而不是使用。

您还需要将 Outlook 添加到您的 VB.Net 项目引用中。


推荐阅读