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

时间:2015-07-23 06:30:13

标签: vba ms-word ms-office

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

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

但我想将所有数学公式转换为图像。如何更改此宏来执行此操作? enter image description here

更新
我尝试了这段代码但不起作用:(没有错误,也没有结果)

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

2 个答案:

答案 0 :(得分:6)

您正在遍历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

答案 1 :(得分:2)

尝试other values for DataType

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