如何将outlook msg文件发送到excel

时间:2016-04-23 20:28:06

标签: vba outlook

以下代码允许我将.msg文件导入excel。 我想知道是否有可能在outlook中创建一个宏,将消息发送到excel文件而不是导入它们。

我用于导入.msg文件的代码如下:

Sub IMPORTMSG()
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.DisplayAlerts = False

    Dim i As Long
    Dim inPath As String
    Dim thisFile As String
'    Dim Msg As MailItem
    Dim ws As Worksheet
'    Dim myOlApp As Outlook.Application
'    Dim MyItem As Outlook.MailItem

    Set myOlApp = CreateObject("Outlook.Application")
    Set ws = ThisWorkbook.Worksheets("A")

    With Application.FileDialog(msoFileDialogFolderPicker)
       .AllowMultiSelect = False
            If .Show = False Then
                Exit Sub
            End If
        On Error Resume Next
        inPath = .SelectedItems(1) & "\"
    End With
    thisFile = Dir(inPath & "*.msg")
    i = 4
    Do While thisFile <> ""
            Set MyItem = myOlApp.CreateItemFromTemplate(inPath & thisFile)
        If MyItem.Subject = "testheader" Then
            ws.Cells(i, 1) = MyItem.Body
            i = i + 1
        End If
            thisFile = Dir()
    Loop

    Set MyItem = Nothing
    Set myOlApp = Nothing

    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.DisplayAlerts = True
End Sub

2 个答案:

答案 0 :(得分:0)

您可以为Outlook创建COM插件并让它运行上面的代码。或者,您可以使用Outlook中的boolean result = check(); //if there is a winner, make it so buttons can't be pressed and new game must start if(result == true){ 集合在Outlook中循环选定的邮件,而不是处理独立的MSG文件。

答案 1 :(得分:0)

您可以开发一个Outlook VBA宏,您可以在其中自动执行Excel并设置Excel单元格值。有关详细信息,请参阅How to automate Microsoft Excel from Visual Basic

另外,您可能会发现Getting Started with VBA in Outlook 2010文章很有帮助。