确定单元格是否包含Word中的等式

时间:2013-08-07 19:59:04

标签: python excel vba ms-word word-vba

我在Word中有一个填充了不同文本的表,以及一些使用Microsoft Equation 3.0的方程式。

我目前正在尝试从表中读取文本并使用相同的表格创建Excel工作表。

有没有办法将Word中的方程式标准化为文本?

如果没有,是否有人知道我如何识别绕过它的等式?

我目前阅读该表的代码如下:

word = win32.gencache.EnsureDispatch('Word.Application')
word.Visible = False
raw_files = glob('*.docx')
xl = win32.gencache.EnsureDispatch('Excel.Application')
ss = xl.Workbooks.Add()
for f in raw_files:
    word.Documents.Open(f)
    doc = word.ActiveDocument
    for x in xrange(1, doc.Paragraphs.Count+1):
        oText = doc.Paragraphs(x)
        if oText.Range.Tables.Count >0 :
            ph = ss.ActiveSheet
            for r in xrange(1, oText.Range.Tables(1).Rows.Count):
                for c in xrange(1, oText.Range.Tables(1).Columns.Count):
                    if oText.Range.Tables(1).Cell(r,c).Range.Text != None:
                        ph.Cells(r+2,c).Value = oText.Range.Tables(1).Cell(r,c).Range.Text

当我遇到等式时的错误是'请求的成员不存在。'

是否有一种简单的方法可以绕过细胞中的等式?

1 个答案:

答案 0 :(得分:1)

如果您正在运行Word 2010(可能是2007),您可以通过这种方式检查单元格中是否存在等式(这是表格中每个单元格的完整循环,您可以轻松转换为您的需求,Word-VBA代码,试用和测试Word 2010):

Dim eqCell As Cell

For Each eqCell In ActiveDocument.Tables(1).Range.Cells

If eqCell.Range.OMaths.Count > 0 Then

    'if there is any equation this if statement will return true
    'so, this cell should be bypassed
    '**EDIT** how to get row and column number of this cell:

    Dim rowNo As Long
    Dim colNo As Long

    rowNo = eqCell.Range.Information(wdEndOfRangeRowNumber)
    colNo = eqCell.Range.Information(wdEndOfRangeColumnNumber)

    Debug.Print rowNo, colNo

    '**END OF EDIT**
End If
Next

您可以尝试使用OMathFunction Object的某些属性来规范化您的等式。不幸的是,我没有这方面的经验。

编辑

要在代码中使用此解决方案,您可以通过几种可能的方式实现它:

a)检查表中是否有任何等式:

If oText.Range.Tables(1).Range.OMath.Count > 0 Then ... '>>here is

b)检查你的细胞中是否有方程式:

If oText.Range.Tables(1).Cell(r,c).Range.OMath.Count > 0 Then ... '>>here is