使用宏将word文档中的公式转换为图像

Convert formula in a word document to image with macro

我有这个宏可以将文档中的所有形状转换为图像:

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

但我想将所有数学公式转换为图像。我怎样才能改变这个宏来做到这一点?

更新:
我试过这段代码但没有用:(没有错误也没有结果)

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

尝试 other values for DataType

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

您正在遍历 InlineShapes 集合,但使用 z 访问 OMaths 集合。那是胡说八道。 那就试试这个:

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

编辑: 下面是一个替代方法,虽然生成的图像质量稍差,但与内嵌公式配合使用效果更好:

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