Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save images in Outlook 2007

Programmatically, of course.

Having already asked this question on superuser, I'm looking at writing a simple macro to pull down the displayed image in an HTML message (email or feed) in outlook 2007, and allow me to save it to disk.

Unfortunately, I havent been able to find where in the OL object model I can reference either linked images, or the html content itself. Finding attached files is easy, its the linked/displayed images that is my issue.

Any help? Of course, if you have a better non-programmatic answer, I'll be glad to see that - over on superuser, of course...

like image 289
AviD Avatar asked Sep 15 '09 18:09

AviD


2 Answers

This is based on the MSDN docs. I don't have Outlook to test it.

Assuming you have an email message open, you can call GetInspector method on the MailItem instance that you have & use its HTMLEditor property to get handle to the DOM.

From here on, you can call regular methods such as document.Images to get handle to all the image elements. I don't know, how one can save it locally to a disk but I am sure, there must be some method to do it.

like image 166
shahkalpesh Avatar answered Nov 06 '22 20:11

shahkalpesh


I had a second look at shahkalpeshs answer and came up with the following solution: (Written in Outlook 2003)

Option Explicit

Private Sub getImages()
    Dim xmlhttp_ As xmlhttp
    Dim htmldoc As Object
    Dim currentImage As Object
    Dim currentResponse() As Byte

    Dim startTime As Date
    Dim maxTime As Long

    Dim pathFolder As String
    Dim pathFull As String
    Dim nrFile As Integer

    pathFolder = "C:\YourFolder\Images\"   '"(small fix for stackoverflow syntaxhighlighter)
    maxTime = 30 ' max time to load 1 File in seconds '

    If Me.ActiveWindow.CurrentItem.GetInspector.EditorType = olEditorHTML Then
        Set htmldoc = Me.ActiveWindow.CurrentItem.GetInspector.HTMLEditor
        Set xmlhttp_ = New xmlhttp

        For Each currentImage In htmldoc.images

            xmlhttp_.Open "GET", currentImage.src
            If Left(currentImage.src, 8) <> "BLOCKED:" Then
                xmlhttp_.Send
                startTime = Now

                pathFull = pathFolder & currentImage.nameProp
                pathFull = Replace(pathFull, "?", vbNullString)
                pathFull = Replace(pathFull, "&", vbNullString)

                Do While xmlhttp_.readyState <> 4
                    If DateTime.DateDiff("s", startTime, Now) > maxTime Then Exit Do
                    DoEvents
                Loop

                If xmlhttp_.readyState = 4 Then
                    If Dir(pathFull) <> "" Then Kill pathFull
                    nrFile = freeFile

                    Open pathFull For Binary As #nrFile
                    currentResponse = xmlhttp_.responseBody
                    Put #nrFile, , currentResponse
                    Close #nrFile
                End If
            End If
        Next currentImage
    End If

    Set xmlhttp_ = Nothing
    Set currentImage = Nothing
    Set htmldoc = Nothing
End Sub

This code will download all the images that are displayed in your ActiveWindow and save them in a folder.

You will need to add a Reference to Microsoft XML (any version >= 2.6 should work) through Tools->References in the VBA Editor

If you want to, you can also set a Reference to the Microsoft HTML Object Library and change:

    Dim htmldoc As Object
    Dim currentImage As Object

to:

    Dim htmldoc As HTMLDocument
    Dim currentImage As HTMLImg

Regarding your comment:

@marg, Thanks for the detailed response. I still cant believe the solution has to be so convoluted - the image is already displaying, why should I have to download it again? And what if I want to save only a single image? (In Outlook 2003, you were able to right click on the image and select save as... now no more.) Since this is the closes to an actual workable solution, and since there doesnt seem to be any better solution in current Outlook - i'm giving you the bounty...

I don't have 2007 to look for a non-programming solution.

I do not believe that the MailItem Object (CurrentItemin my solution is a MailItem) differs much between the versions (but I base that assumption on 0 % research :D) and I was not able to locate the direct local path where the displayed images are stored even though I am pretty sure they should be in your browsers cache-folder. Crawling your folder for a file with the name currentImage.nameProp and copying it to your destination folder would be an alternative solution. Simply redownloading the image should not be that bad :D

like image 35
marg Avatar answered Nov 06 '22 20:11

marg