excel - 无法打开 .msg 文件

标签 excel vba outlook msg

我有大约 90 个 .msg、outlook 文件需要打开,将 excel 附件转换为 .csv 文件并保存。目前,下面的代码是简单打开.msg Outlook文件,但出现错误:enter image description here

如何允许打开 .msg 文件。

脚本:

Sub OpenMSGRenameDownloadAttachement()

    Dim objOL As Outlook.Application
    Dim Msg As Outlook.MailItem

    Dim MsgCount As Integer

    Set objOL = CreateObject("Outlook.Application")

    'Change the path given month, ie. do this for Jan, Feb, April
    inPath = "C:\January Messages"

    thisFile = LCase(Dir(inPath & "\*.msg"))
    Do While thisFile <> ""

        Set Msg = objOL.Session.OpenSharedItem(thisFile)

        Msg.Display

        MsgBox Msg.Subject
        thisFile = Dir
    Loop

    Set objOL = Nothing
    Set Msg = Nothing

End Sub

最佳答案

尝试这个:

Sub OpenMSGRenameDownloadAttachement()
Dim Msg As Outlook.MailItem
Dim objAtt As Outlook.Attachment
Set objOL = CreateObject("Outlook.Application")
Set objNs = objOL.GetNamespace("MAPI")
'objNs.Logon

inPath = "C:\January Messages\"
outPath = "C:\January Messages\attachments\" 'create this folder for attachments or use your own
thisFile = Dir(inPath & "*.msg")

Do While Len(thisFile) > 0
    Set Msg = objNs.OpenSharedItem(inPath & thisFile)
    'MsgBox inPath & thisFile
    'MsgBox Msg.Subject
    'MsgBox Msg.SenderEmailAddress
    'MsgBox Msg.Recipients.Item(1).Address
    For Each objAtt In Msg.Attachments
        If Right(objAtt, 4) = "xlsx" Or Right(objAtt, 3) = "xls" Then
            objAtt.SaveAsFile outPath & Split(objAtt.DisplayName, ".")(0) & ".csv"
        End If
    Next
    thisFile = Dir
Loop

Set objOL = Nothing
Set objNs = Nothing
End Sub

关于excel - 无法打开 .msg 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43748113/

相关文章:

vba - 如果另一个单元格等于某个值,则使用 VBA 更改一个单元格的值

Excel VBA - 条件格式中的用户定义函数

ms-access - USING 语句的 VBA 是什么

c# - HTML 未显示在 outlook 邮件中

node.js - 无法使用 Nodemailer 连接到 outlook.com SMTP

vba - 如何在 Excel VBA 中创建自动动态折线图

node.js - 用于写入 excel 文件的 Node XLSX

html - Outlook 2010 与 Outlook 2013 的电子邮件 html 模板

java程序读取特定数据

excel - 过滤唯一值并从 A 到 Z Excel VBA 排序