Outlook VBA Replace inline object with text

独自空忆成欢 提交于 2019-11-29 17:57:33

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!