代码之家  ›  专栏  ›  技术社区  ›  AviD

在Outlook 2007中保存图像

  •  6
  • AviD  · 技术社区  · 15 年前

    当然是程序化的。

    已经问过了 this question 在superuser上,我正在考虑编写一个简单的宏来下拉outlook 2007中HTML消息(电子邮件或提要)中显示的图像,并允许我将其保存到磁盘。

    不幸的是,我在OL对象模型中找不到可以引用链接图像或html内容本身的位置。查找附加文件很容易,链接/显示的图像是我的问题。

    有什么帮助吗?当然,如果你有一个更好的非编程的答案,我会很高兴看到-在超级用户,当然。。。

    2 回复  |  直到 8 年前
        1
  •  2
  •   shahkalpesh    14 年前

    这是基于MSDN文档。我没有Outlook来测试它。

    如果你有一封电子邮件,你可以打电话给 GetInspector 方法论 MailItem 使用它的实例 HTMLEditor property 去处理DOM。

    从现在起,您可以调用常规方法,如 document.Images 来处理所有的图像元素。我不知道,如何将它保存到本地磁盘上,但我确信,一定有办法做到这一点。

        2
  •  1
  •   marg    14 年前

    我又看了一眼shahkalpeshs的回答,得出了以下解决方案: (写在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
    

    此代码将下载 显示 在你的 ActiveWindow 把它们保存在一个文件夹里。

    您需要添加对 Microsoft XML (任何版本>=2.6都可以)通过VBA编辑器中的工具->引用

    如果需要,还可以设置对 微软HTML对象库 并更改:

        Dim htmldoc As Object
        Dim currentImage As Object
    

    致:

        Dim htmldoc As HTMLDocument
        Dim currentImage As HTMLImg
    

    关于你的评论:

    @玛格,谢谢你的详细回复。我仍然不敢相信解决方案必须如此复杂-图像已经显示,为什么我必须再次下载它?如果我只想保存一张图片呢?(在Outlook 2003中,您可以右键单击图像并选择另存为。。。因为这是一个接近实际可行的解决方案,而且在当前的前景中似乎没有更好的解决方案-我给你赏金。。。

    我没有2007年寻找非编程解决方案。

    我不相信 MailItem 对象( CurrentItem 在我的解决方案中是 邮件 )不同的版本之间有很大的不同(但我基于0%的研究:D)并且我无法找到显示图像存储的直接本地路径,即使我非常确定它们应该在浏览器缓存文件夹中。正在爬网文件夹中的文件名 currentImage.nameProp 将其复制到目标文件夹将是另一种解决方案。简单地重装映像不应该那么糟糕:D

    推荐文章