0
votes

I'm using a script (that I didn't write) to convert Word docs to XML. Everything works perfectly as long as the Word doc is consistently styled. One of the things that the script can't handle is when images are inserted as OLE objects. These images are simply ignored by the script, whereas images that were inserted regularly, as non-OLE objects, are picked up by the script just fine.

I've also been using a VBA script (see part of it below) to clean up the document prior to transforming it. The script adds styles to the document, moves some things, and...what I'm trying to make it do is convert OLE images to non-OLE images.

So what I want to know is: is there a way to iterate through the inlineShapes in the document and convert OLE graphics to regular graphics or, is there a way to get the image data out of the OLE object and paste it into the document as a PNG.

This is what my vba script does right now:

Sub docscrubber()

Dim i  As Integer
Dim total  As Integer
Dim oIshp As InlineShape

total = ActiveDocument.InlineShapes.Count
i = 0
For Each oIshp In ActiveDocument.InlineShapes
    i = i + 1
    On Error Resume Next
    Application.StatusBar = "Progress: " & i & " of " & total
        With oIshp

         oIshp.Range.Copy
         oIshp.Select
         Selection.Paste

        End With
Next oIshp

End Sub

Instead of getting the image data, I get the document code.

Thanks in advance!

1

1 Answers

1
votes

Since I don't know what OLE server is linked to these, and probably don't even have that, I can't do a definitive test, however...

OLE objects in Word, the same as with any dynamic information, are managed by field codes. You can toggle the display of field codes on and off using Alt+F9. For the formats I tested, these were EMBED fields.

To turn field content into static content (the OLE objects into their graphic representation) unlinking the field should suffice. Based on the code you show us, something like this seems likely:

For Each oIshp In ActiveDocument.InlineShapes
    i = i + 1
    'On Error Resume Next ' do NOT use this!
    Application.StatusBar = "Progress: " & i & " of " & total
    With oIshp   
       If oIshp.Range.Fields.Count = 1 Then
         oIshp.Range.Fields.Unlink
       End If 
    End With
Next oIshp