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

标签 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

最佳答案

您正在迭代 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

关于vba - 使用宏将word文档中的公式转换为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31579710/

相关文章:

c# - 使用 Word.Interop 创建多级项目符号列表

.net - 以编程方式将页脚添加到 Office Word/Excel 文档

VBA在多个工作表上设置图表不可见Excel

r - 有没有办法在 R 中为 Word 制作漂亮的表格?

.net - 那么,.NET 没有内置的 Office 功能吗?

c# - MS Word 2010 无法打开宏存储

powershell - 在单词中为表格的特定单元格着色

excel - 如何使用 VBA 将符号/图标格式化为单元格而不使用条件格式

ms-access - 在内存中,独立的,断开连接的 ADO 记录集

vba - 将 Range 属性存储为对象?