2010-06-22 109 views
0

我有一個腳本來打開附件並將它們附加到郵件正文。我有它的文本文件工作,但我也需要它爲.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 

回答

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 
1

要閱讀的內容.msg文件,我使用了以下方法。

  1. 使用CreateItemFromTemplate使用Outlook對象

  2. 使用此Outlook對象將數據保存到一個文本文件

  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文件根據您的計算需要使用它。