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

发布于 2024-09-07 06:49:41 字数 2033 浏览 14 评论 0原文

我有一个脚本可以打开附件并将其附加到邮件正文中。我让它适用于文本文档,但我也需要它适用于 .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

I have a script to open attachments and append them to the message body. I have it working for text documents but I need it working for .msg attachments too.

At the moment it just doesn't read the object. Can anyone help?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落叶缤纷 2024-09-14 06:49:41

您应该考虑使用 Outlook 兑换

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

You should look into using Outlook Redemption.

Also, you should look at this documentation for format specificaion
http://msdn.microsoft.com/en-us/library/cc463912.aspx

情绪操控生活 2024-09-14 06:49:41

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

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

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

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

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 文件,并根据计算需要使用它。

To read the contents of a .msg file, I have used the following approach.

  1. Use CreateItemFromTemplate for the msg file using the Outlook object

  2. Use this Outlook object to save the data into a .txt file

  3. Once the .txt file is created, read it and use the data as required

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 

Once the .txt file is created use it as required for your computations.

一片旧的回忆 2024-09-14 06:49:41
    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
    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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文