• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 858
  • Last Modified:

Embedded Msg, in Outlook, with a Word Doc - how to get at Embedded Msg as Attachment?

I have code that I have experimented with, that is looking at a particular Mail folder, and looking for infomation within an attachment, that has an attachment.
An Email in the inbox, has an embedded email message, that has a Word Document attached.
So, Last Sender, sends an email, from First Sender that is an embedded attachment, and There is
a Word Document from First Sender.

I can get to the embedded email- of First Sender which looks like a FILE with an extension of .Msg
and I can save that to disk, and when I open it from disk, I see the word Doc attachment.

 Dim objOutLook As Outlook.Application
     Dim objNameSpace As Outlook.NameSpace
     Dim objFolders As Outlook.Folders
     Dim objFolder As Outlook.MAPIFolder
     Dim objItem As Outlook.MailItem
     Dim objAttachment As Attachment
     Dim objParmAttachment As Object
     Dim objFSO As FileSystemObject
     Dim strSaveToPath As String
     Dim strFilename As String
     Dim intCount As Integer
   
   
    strSaveToPath = "C:\Data\Test\"
         Set objOutLook = CreateObject("Outlook.Application")
     Set objNameSpace = objOutLook.GetNamespace("MAPI") ' Reference Outlook namespace
     Set objFolders = objNameSpace.Folders.Item("Mailbox - Support").Folders
     If objFolders.Count > 0 Then
     Set objFolder = objFolders.Item("Inbox")
     '* the objFolder of the Inbox has the Items to be Processed
     '* We can then get access to each item by looping through the inbox
     '* And we can determine if there are attachements in the item.
     Set objItem = objFolder.Items(1)
     If objItem.Attachments.Count > 0 Then
        Set objAttachment = objItem.Attachments.Item(1)
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        strFilename = objAttachment.FileName
        intCount = 1
        Do While True
            If objFSO.FileExists(strSaveToPath & strFilename) Then
                strFilename = objFSO.GetBaseName(objAttachment.FileName) & "(" & intCount & ")." & objFSO.GetExtensionName(objAttachment.FileName)
                intCount = intCount + 1
            Else
                objAttachment.SaveAsFile strSaveToPath & strFilename
                Exit Do
            End If
        Loop
       
     End If

.......... END OF CODE CLIP

I am hitting my head on the wall trying to see how to get this.
0
mitchellm44
Asked:
mitchellm44
  • 4
  • 2
1 Solution
 
Robberbaron (robr)Commented:
the embedded msg is a special type of attachment.  objAttachment.Type
see http://support.microsoft.com/kb/231958

CdoFileData = 1: CdoEmbeddedMessage = 4

     Set objItem = objFolder.Items(1)
     If objItem.Attachments.Count > 0 Then
        Set objAttachment = objItem.Attachments.Item(1)
        if objAtachment.Type = CdoEmbeddedMessage Then
                Set objEmbeddedMessage = objMessage.Attachments(1).Source
                'now look at the attachments of embedded message
                if objEmbeddedMessage.Attachments.Count>0 then
                      Set objEMAttachment = objEmbeddedMessage.Attachments.Item(1)
                      if objEMAttachment.Type = CdoFileData Then
                             'can be saved ....
 
                      end if
                end if
        end if
     end if


0
 
mitchellm44Author Commented:
Your answer is interesting, I had been using the Outlook library, to get the addressability to the Attacment, and your approach uses the CDO Library, which I had not thought of. In looking at the object browser, the CDO library shows up as MAPI (which is what is being used behind the scenes anyway) and the Attachment object with each class (Outlook.Attachment, and MAPI.Attachment) have different methods.

The change between the two libraries, does make a change from the start of the code, as it requires the Session to be established from the start as a MAPI session and not an Outlook front end to a MAPI session (which should be aCleaner approach).

The KB article link has a code example, that is dated from 2004. That example has a MAPI.Login, and sets an object to an "inbox" which does not exist
in the CDO 1.21 library (in my machine) - so the objSession.Inbox is no longer there.

Your code example picked up after that work, so I reworked my startup to get to something where you started.
Unfortunately I am at the same point -- I have an attachment that is an embedded EMAIL - but its file type is actually cdoFileData - not EmbeddedMessage.
I can see  that it is a message from the ".msg" file extension in the name field.  

Here is the reworked code.

     '* Reference to FileScripting - Microsoft Scripting Runtime Library
     '* Reference to Microsoft CDO 1.21 Library
     Dim objMAPI As MAPI.Session
     
     Dim objInfostore As MAPI.InfoStore
     Dim objMAPIAttachment As MAPI.Attachment
     Dim objMAPIfolders As MAPI.Folders
     Dim objMAPIfolder As MAPI.Folder
     Dim objMAPIMessages As MAPI.Messages
     Dim objMAPIMessage As MAPI.Message
     Dim objEmbeddedMessage As MAPI.Message
     Dim objMAPIAttachments As MAPI.Attachments
   
     Dim objFSO As FileSystemObject
     Dim strSaveToPath As String
     Dim strFilename As String
     Dim intCount As Integer
     strSaveToPath = "C:\Data\Test\"
    Set objMAPI = CreateObject("MAPI.Session")
    objMAPI.Logon ("%username%")
    Set objInfostore = objMAPI.InfoStores("Mailbox - Support")
    Set objMAPIfolder = objInfostore.RootFolder
    Set objMAPIfolders = objMAPIfolder.Folders
    Set objMAPIfolder = objMAPIfolders("Inbox")
    Set objMAPIMessages = objMAPIfolder.Messages
   
    Set objMAPIMessage = objMAPIMessages.Item(1) '*First message
    '* Get attachments for Message
    'objMAPIMessage.ConversationTopic and Subject might have user info
     
       
    Set objMAPIAttachments = objMAPIMessage.Attachments
    If objMAPIAttachments.Count > 0 Then
       
        Set objMAPIAttachment = objMAPIAttachments.Item(1)
        If objMAPIAttachment.Type = MAPI.CdoEmbeddedMessage Then
        Else
           If objMAPIAttachment.Type = MAPI.CdoFileData Then
            '* Looks like this is actually the embedded EMAIL
            '* Name has an .msg when there is the Embedded email
               If InStr(1, objMAPIAttachment.Name, ".msg") > 0 Then
                  strFilename = objMAPIAttachment.Name
                  objMAPIAttachment.WriteToFile (strSaveToPath & strFilename)
                  '** But there is not a way to get at that Message.
                  '* I need to open that message as IT contains a word doc.
                  '* Which I can see if I open the saved file
               End If
           End If
        End If
    End If
0
 
mitchellm44Author Commented:
One more thought.. The possiblity of creating a new message and putting it into the INBOX was a thought.. however the KB article code below has nothing in SOURCE to use..
...
If objMessage.Attachments.Count > 0 Then
            If objMessage.Attachments(1).Type = CdoEmbeddedMessage Then
                Set objEmbeddedMessage = objMessage.Attachments(1).Source
                Set objNewMail = objEmbeddedMessage.CopyTo(objInbox.ID)
                objNewMail.Update
                Set objNewMail = Nothing
                Set objEmbeddedMessage = Nothing
            End If
        End If
.....
Any thoughts?
0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
mitchellm44Author Commented:
I finally found the missing piece to what I needed. And from the Outlook approach.
On the WebDAV 101 blog, which today has a URL of
http://blogs.msdn.com/webdav_101/archive/2007/05/04/looking-for-a-way-to-save-a-msg-file-or-load-a-msg-file.aspx
The code lines that basically indicated -- Save the file, and then OPEN AS TEMPLATE as a new item.
Contains the attachment, and then I can get at the embedded word doc.



    '** Found sample of Creating a message here
    '** http://blogs.msdn.com/webdav_101/archive/2007/05/04/looking-for-a-way-to-save-a-msg-file-or-load-a-msg-file.aspx
   
     Dim objOutLook As Outlook.Application
     Dim objNameSpace As Outlook.NameSpace
     Dim objFolders As Outlook.Folders
     Dim objFolder As Outlook.MAPIFolder
     Dim objItem As Outlook.MailItem
     Dim objAttachment As Outlook.Attachment
     
     Dim objNewItem As Outlook.MailItem
     
     Dim objFSO As FileSystemObject
     Dim strSaveToPath As String
     Dim strFilename As String
     Dim intCount As Integer
        'Infostore - RootFolder - Messages - Item 1 - Attachments
         Set objOutLook = CreateObject("Outlook.Application")
     Set objNameSpace = objOutLook.GetNamespace("MAPI") ' Reference Outlook namespace
     Set objFolders = objNameSpace.Folders.Item("Mailbox - Support").Folders
     If objFolders.Count > 0 Then
     Set objFolder = objFolders.Item("Inbox")
     '* the objFolder of the Inbox has the Items to be Processed
     '* We can then get access to each item by looping through the inbox
     '* And we can determine if there are attachements in the item.
     Set objItem = objFolder.Items(1)
     If objItem.Attachments.Count > 0 Then
        Set objMAPIAttachment = objItem.Attachments.Item(1)
        If objMAPIAttachment.Type = MAPI.CdoEmbeddedMessage Then
            '*

        End If
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        strFilename = objMAPIAttachment.FileName
        intCount = 1
        Do While True
            If objFSO.FileExists(strSaveToPath & strFilename) Then
                strFilename = objFSO.GetBaseName(objAttachment.FileName) & "(" & intCount & ")." & objFSO.GetExtensionName(objAttachment.FileName)
                intCount = intCount + 1
            Else
                objMAPIAttachment.SaveAsFile strSaveToPath & strFilename
                Exit Do
            End If
        Loop

        '* The next line is the missing piece of the puzzle - getting at the Insides of the message
        Set objNewItem = objOutLook.CreateItemFromTemplate(strSaveToPath & strFilename)
        '* NewItem has the attachment and we have access to the Attachment within.
        MsgBox objNewItem.Attachments.Item(1).FileName
        '** This attachment IS the word Doc I am looking for
       
     End If
0
 
Robberbaron (robr)Commented:
fine by me.
0
 
mitchellm44Author Commented:
Slight code correction to save the file correct. This does work

  Do While True
            If objFSO.FileExists(strSaveToPath & strFileName) Then
                strFileName = objFSO.GetBaseName(objAttachment.FileName) & "(" & intCount & ")." & objFSO.GetExtensionName(objAttachment.FileName)
                intCount = intCount + 1
            Else
                objNewItem.Attachments.Item(1).SaveAsFile (strSaveToPath & strFileName) '*FIXED LINE
                Exit Do
            End If
        Loop
0
 
Vee_ModCommented:
Closed, 500 points refunded.
Vee_Mod
Community Support Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now