VBA【公式】依据 “=”将多行矩阵公式变成单行公式

背景

软件OCR生成的公式,有时候公式糊成一块了。

目的

将糊成一块的多行公式一行行分开(并且只对糊的公式操作),操作完后标记颜色为红色

解决步骤

1. 属性:ActiveDocument.OMaths.ParentFunction

ActiveDocument.OMaths的 ParentFunction

  • 如果公式为 多行矩阵
    ActiveDocument.OMaths.ParentFunction 返回对象
  • 如果公式为单行
    ActiveDocument.OMaths.ParentFunction 返回Nothing

然后 多行矩阵 变成 单行 用到 .ParentFunction.Remove,有了这个remove方法,后面的详细实现就好办了…

2. 编写VBA

依据多行公式前的等于号=,来作为分割依据

Sub test20240322【公式】word公式多行变单行()
    '关键点:oneOMath.ParentFunction.Remove
    '判断:等于号=
    '难点:熟练OMaths各属性方法
    Dim lngEnd&, count1%
    Dim oneOMath As Word.OMath, mathFun As OMathFunction 'ParentFunction的返回对象
    For Each oneOMath In ActiveDocument.OMaths
        Set mathFun = oneOMath.ParentFunction '设置对象,当无矩阵时,该值为Nothing
        If Not (mathFun Is Nothing) Then
            mathFun.Range.Select
            mathFun.Range.Font.ColorIndex = wdRed
            lngEnd& = mathFun.Range.End
            mathFun.Remove
            Do
                Selection.MoveRight unit:=wdCharacter, count:=2 '数量2 能跳转到公式域内
                count1% = Selection.MoveEndUntil("=", 200) '设置最大范围200
                If count1% > 1 And Selection.End < lngEnd& Then 'count1%大于0不能公式一行只有一个字符  扩选范围大于当前公式时取消执行
                    Selection.InsertParagraphAfter
                End If
                Selection.OMaths(1).Type = wdOMathInline '公式更改为嵌入
            Loop Until count1% = 0 Or Selection.End >= lngEnd&
        End If
    Next
    Set mathFun = Nothing '取消对象引用,释放对象
End Sub

以上就是编写的VBA代码,测试有效分享给大家。执行后 便能变成单行公式。

3. 效果预览

总结

此函数目前只能根据 多行公式前的等于号= 来判断,可能会有没有改到的情况,水平所限只能这样了

附录:关于公式的MathML

下面是某矩阵公式:

公式MathML为:

<math xmlns='http://www.w3.org/1998/Math/MathML'>
	<mrow>
		<mtable equalrows='true' equalcolumns='true'>
			<mtr>
				<mtd>
					<mrow>
						<mo>&#x00A0;</mo>
						<mo>=</mo>
						<mn>3</mn>
						<mi>a</mi>
						<mo>+</mo>
						<mn>4</mn>
						<mi>a</mi>
						<mo>+</mo>
						<mn>6</mn>
						<mi>b</mi>
						<mo>&#x2212;</mo>
						<mn>8</mn>
						<mi>b</mi>
						<mo>+</mo>
						<mn>10</mn>
						<mi>a</mi>
					</mrow>
				</mtd>
			</mtr>
			<mtr>
				<mtd>
					<mrow/>
				</mtd>
				<mtd>
					<mrow>
						<mo>&#x00A0;</mo>
						<mo>=</mo>
						<mn>17</mn>
						<mi>a</mi>
						<mo>&#x2212;</mo>
						<mn>2</mn>
						<mi>b</mi>
						<mo>;</mo>
					</mrow>
				</mtd>
			</mtr>
		</mtable>
	</mrow>  
</math>

执行Remove函数后,变成如下效果:

其MathML变成:

<math>
	<mrow>
		<mtext>&#x00A0;</mtext>
		<mo>&#x00A0;</mo>
		<mo>=</mo>
		<mn>3</mn>
		<mi>a</mi>
		<mo>+</mo>
		<mn>4</mn>
		<mi>a</mi>
		<mo>+</mo>
		<mn>6</mn>
		<mi>b</mi>
		<mo>&#x2212;</mo>
		<mn>8</mn>
		<mi>b</mi>
		<mo>+</mo>
		<mn>10</mn>
		<mi>a</mi>
		<mtext>&#x00A0;&#x00A0;</mtext>
		<mo>&#x00A0;</mo>
		<mo>=</mo>
		<mn>17</mn>
		<mi>a</mi>
		<mo>&#x2212;</mo>
		<mn>2</mn>
		<mi>b</mi>
		<mo>;</mo>
	</mrow>
</math>

观察发现 MathML的 mtable mtr mtd 都不见了,留下了 mrow 。单行公式大概原理就是这样。

版权声明:本文为博主作者:三人两堆 shi原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/sanrenliangduishi/article/details/136984826

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2024年4月22日
下一篇 2024年4月22日

相关推荐