2010-06-22 10 views
0

J'ai un script pour ouvrir les pièces jointes et les ajouter au corps du message. Je l'ai pour les documents texte, mais j'en ai besoin pour travailler avec les pièces jointes .msg aussi.Ajout du contenu de la pièce jointe .msg au corps du message

Pour le moment, il ne lit pas l'objet. Quelqu'un peut-il aider?

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 

Répondre

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

Pour lire le contenu d'un Fichier .msg, j'ai utilisé l'approche suivante.

  1. Utilisez CreateItemFromTemplate pour le fichier msg en utilisant l'objet Outlook

  2. Utilisez ce objet Outlook pour enregistrer les données dans un fichier txt

  3. Une fois le fichier txt est créé, lire et utiliser les données requises

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 

Une fois que t Le fichier .txt est créé et utilisé comme requis pour vos calculs.

Questions connexes