Convert formula in word document to image using macro

I have this macro to convert all shapes in a document to an image:

Dim i As Integer, oShp As Shape

For i = ActiveDocument.Shapes.Count To 1 Step -1
    Set oShp = ActiveDocument.Shapes(i)
    oShp.Select
    Selection.Cut
    Selection.PasteSpecial Link:=False, dataType:=wdPasteEnhancedMetafile, _
        Placement:=wdInLine, DisplayAsIcon:=False
Next i

      

But I want to convert all math formulas to image. How can I modify this macro to do this? enter image description here

UPDATE:
I tried this code but didn't work: (No error and also no result)

Sub AllEquationToPic()
Dim z As Integer, equation As OMath

For z = ActiveDocument.InlineShapes.Count To 1 Step -1
    Set equation = ActiveDocument.OMaths(z)
        equation.Range.Select
        Selection.Cut
        Selection.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
            Placement:=wdInLine, DisplayAsIcon:=False
Next z
End Sub

      

+3


source to share


2 answers


You are iterating through the collection InlineShapes

, but using z

to access the collection OMaths

. This is nonsense. Try it:

Sub AllEquationToPic()
Dim z As Integer, equation As OMath

For z = ActiveDocument.OMaths.Count To 1 Step -1
    Set equation = ActiveDocument.OMaths(z)
        equation.Range.Select
        Selection.Cut
        Selection.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
            Placement:=wdInLine, DisplayAsIcon:=False
Next z
End Sub

      



Edit: Here's an alternative that works better with inline formulas, albeit with slightly poorer image quality:

Sub FormulaDoc2PicDoc()
Dim doc As Document, docPath As String, htmPath As String
Dim alertStatus

alertStatus = Application.DisplayAlerts
Application.DisplayAlerts = wdAlertsNone

Set doc = ActiveDocument
docPath = doc.FullName
htmPath = docPath & ".htm"

doc.SaveAs htmPath, wdFormatFilteredHTML
doc.Close False

Application.DisplayAlerts = alertStatus

Set doc = Documents.Open(htmPath, False)

End Sub

      

+6


source


Try other values ​​for DataType



  • wdPasteBitmap
  • wdPasteDeviceIndependentBitmap
  • wdPasteEnhancedMetafile
  • wdPasteHTML
  • wdPasteHyperlink
  • wdPasteMetafilePicture
  • wdPasteOLEObject
  • wdPasteRTF
  • wdPasteShape
  • wdPasteText
+2


source







All Articles