将.msg附件的内容附加到邮件正文

时间:2010-06-22 15:05:18

标签: vba outlook outlook-vba email-attachments msg

我有一个脚本来打开附件并将它们附加到邮件正文中。我有它为文本文档工作,但我也需要它为.msg附件工作。

目前它只是没有读取对象。有人可以帮忙吗?

Sub RunAScriptRuleRoutine(MyMail As MailItem)

Dim strID As String
Dim olNS As Outlook.NameSpace
Dim olMail As Outlook.MailItem
Dim olMailAT As Outlook.MailItem

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set olMail = olNS.GetItemFromID(strID)

If olMail.Subject = "lala" Then

    If olMail.Attachments.Count > 0 Then

        Dim strLine As String
        Dim mailLine As String
        Dim strLines As String

        For i = 1 To olMail.Attachments.Count

            strFileName = "C:\emailTemp\" + olMail.Attachments.Item(i).FileName

            If InStr(strFileName, "msg") Then

                olMail.Attachments.Item(i).SaveAsFile strFileName
                strLines = strLines + "//Start of " + strFileName + " //" + vbCrLf

                Open strFileName For Input As #1
                    Do While Not EOF(1)
                    Line Input #1, strLine
                        mailLine = mailLine + strLine
                    Loop
                Close #1

                olMailAT = mailLine
                strLine = objMailAT.Body
                strLines = strLines + "heres the .msg" + vbCrLf
                strLines = strLines + "//End of " + strFileName + " //" + vbCrLf

            Else

                olMail.Attachments.Item(i).SaveAsFile strFileName

                strLines = strLines + "//Start of " + strFileName + " //" + vbCrLf
                Open strFileName For Input As #1
                    Do While Not EOF(1)
                    Line Input #1, strLine
                        strLines = strLines + vbCrLf + strLine
                    Loop
                Close #1
                strLines = strLines + "//End of " + strFileName + " //" + vbCrLf

            End If

        Next

        'save to email body and save email
        olMail.Body = strLines
        olMail.Save

    End If

End If

Set olMail = Nothing
Set olNS = Nothing

End Sub

4 个答案:

答案 0 :(得分:1)

您应该考虑使用Outlook Redemption

另外,您应该查看此文档以了解格式规范 http://msdn.microsoft.com/en-us/library/cc463912.aspx

答案 1 :(得分:1)

要阅读.msg文件的内容,我使用了以下方法。

  1. 使用Outlook对象为msg文件使用CreateItemFromTemplate

  2. 使用此Outlook对象将数据保存到.txt文件

  3. 创建.txt文件后,将其读取并根据需要使用数据

  4. Dim OL : Set OL=CreateObject("Outlook.Application")
    Dim Msg ':Set Msg= CreateObject("Outlook.MailItem")
    Set Msg = OL.CreateItemFromTemplate("C:\test.msg")
    'MsgBox Msg.Subject
    Msg.saveAs "C:\test.txt", olDoc
    'The above statement will save the contents of .msg 
    'file into the designate .txt file
    Set OL = Nothing
    Set Msg = Nothing 
    

    创建.txt文件后,请根据您的计算使用它。

答案 2 :(得分:0)

答案 3 :(得分:0)

    Function GetCurrentItem() As Object
     Dim objApp As Outlook.Application
     Set objApp = Application
     On Error Resume Next
     Select Case TypeName(objApp.ActiveWindow)
     Case "Explorer"
     Set GetCurrentItem = _
     objApp.ActiveExplorer.Selection.Item(1)
     Case "Inspector"
     Set GetCurrentItem = _
     objApp.ActiveInspector.CurrentItem
     Case Else
     End Select
    End Function
 'This is how you read the attachment using your path "strFileName"
   Set OL = New Outlook.Application
   Set myMessage = OL.CreateItemFromTemplate(strFileName)
   myMessage.Display
   Set msg2 = GetCurrentItem()
   MsgBox(msg2.Body)
   mg2.Close 1