Outlook VBA Replace inline object with text

后端 未结 1 1227
孤独总比滥情好
孤独总比滥情好 2020-12-22 08:06

I have an email message in my Inbox which contains an inline object (e.g., an image). I want to remove it, and insert text at the same point in the email.

I tried wi

相关标签:
1条回答
  • 2020-12-22 08:32

    The WordEditor basically is a word Document if I remember correctly, so you should be able to do something similar to (tested in Word, may need tweak for Outlook), assuming an object variable like doc to represent the Document:

    Revised & tested in Outlook 2010

    Dim shp as InlineShape
    Dim doc as Object `Word.Document
    Dim shpRange as Object `Word.Range
    Const wdInlineShapePicture as Long = 3
    Const wdInlineShapesEmbeddedOLEObject as Long = 1
    Set doc = objMsg.GetInspector.WordEditor
    For Each shp In doc.InlineShapes
        Select Case shp.Type 
            Case wdInlineShapePicture, wdInlineShapesEmbeddedOLEObject
                '## Assign a range object with the text position of the shape
                Set shpRange = doc.Range(shp.Range.Characters.First.Start, _
                                      shp.Range.Characters.Last.End)
                '## Replace the shape with text:
                shpRange.Text = "Replacement Text"
            Case Else
                '## Do something else for other shape types, etc.
          End Select
    
    Next
    

    Here is an example macro to process incoming mailitems, and replace the embedded images with text. Note the need to UnProtect the document:

    Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
        Dim arr() As String
        Dim i As Integer
        Dim m As MailItem
        '## Word objects, using late-binding (or enable reference to MS Word)
        Dim shp As Object 'Word.InlineShape
        Dim doc As Object 'Word.Document
        Dim shpRange As Object 'Word.Range
        '## Establish some word constants for use with late-binding
        Const wdInlineShapePicture As Long = 3
        Const wdInlineShapeEmbeddedOLEObject As Long = 1
        Const wdInlineShapeLinkedPicture As Long = 4
    
        arr = Split(EntryIDCollection, ",")
        For i = 0 To UBound(arr)
            Set m = Application.Session.GetItemFromID(arr(i))
            Set doc = m.GetInspector.WordEditor
            doc.UnProtect
            For Each shp In doc.InlineShapes
                Select Case shp.Type
                    Case wdInlineShapePicture, _
                         wdInlineShapeEmbeddedOLEObject, _
                         wdInlineShapeLinkedPicture
    
                        '## Assign a range object with the text position of the shape
                        Set shpRange = doc.Range(shp.Range.Characters.First.Start, _
                                                  shp.Range.Characters.Last.End)
                        '## Replace the shape with text:
                        shpRange.Text = "Replacement Text"
                    Case Else
    
                End Select
            Next
        Next
    End Sub
    
    0 讨论(0)
提交回复
热议问题