o数学从 excel 到单词
oMath from excel to word
我正在通过 excel VBA 创建一个单词报告。我需要编写数学方程式,但不幸的是,word 文档不会自动更正 \pi 和 \times。否则打印等式。有人可以建议我前进的方向是什么。下面是代码
Sub AreaSolidBolt(wrdApp As Object, wrdDoc As Object, d As Variant)
Dim objRange As Object
Dim objEq As OMath
Dim aCorrect As OMathAutoCorrectEntry
wrdApp.OMathAutoCorrect.UseOutsideOMath = True
Set objRange = wrdDoc.Range
objRange.Text = "A = \pi/4 \times d^2"
Set objRange = wrdApp.Selection.OMaths.Add(objRange)
For Each aCorrect In wrdApp.OMathAutoCorrect.Entries
With objRange
If InStr(.Text, aCorrect.Name) > 0 Then
.Text = Replace(.Text, aCorrect.Name, aCorrect.Value)
End If
End With
Next aCorrect
Set objEq = objRange.OMaths(1)
objEq.BuildUp
Set objRange = Nothing
End Sub
我在调用函数中定义了如下对象。你能告诉我前进的方向吗?
Set fso = CreateObject("Scripting.FileSystemObject")
Set wrdApp = CreateObject("Word.Application")
If Not fso.FileExists(wrdFileName) Then
Set wrdDoc = wrdApp.Documents.Add
wrdApp.Visible = False
With wrdDoc
.SaveAs FileName:=wrdFileName
End With
Else
Set wrdDoc = wrdApp.Documents.Open(wrdFileName)
wrdApp.Visible = False
wrdDoc.Content.InsertAfter vbLf
End If
如果自动更正无法识别您需要使用的内容,那么您可以使用 ChrW() 写入相应的 (unicode) 字符代码作为等式的一部分。例如PI为960。
不清楚您认为 "times" 字符是什么,"x" 是“*”还是其他字符。我注意到大多数数学方程式实际上并不使用字符进行乘法运算,这可能就是自动更正功能不接受该字符的原因。但是您当然可以直接输入这些内容吗?
例如:
objRange.Text = "A = " & ChrW(960) & "/4 * d^2"
我自己找到了答案。略有修改。错误与代码行的位置有关
设置 objRange = wrdApp.Selection.OMaths.Add(objRange)
下面是修改后的代码。
Sub AreaSolidBolt(wrdApp As Object, wrdDoc As Object, d As Variant)
Dim objRange As Object
Dim objEq As OMath
Dim aCorrect As OMathAutoCorrectEntry
wrdApp.OMathAutoCorrect.UseOutsideOMath = True
Set objRange = wrdDoc.Range
objRange.Text = "A = \pi/4 \times d^2"
For Each aCorrect In wrdApp.OMathAutoCorrect.Entries
With objRange
If InStr(.Text, aCorrect.Name) > 0 Then
.Text = Replace(.Text, aCorrect.Name, aCorrect.Value)
End If
End With
Next aCorrect
Set objRange = wrdApp.Selection.OMaths.Add(objRange)
Set objEq = objRange.OMaths(1)
objEq.BuildUp
Set objRange = Nothing
End Sub
我正在通过 excel VBA 创建一个单词报告。我需要编写数学方程式,但不幸的是,word 文档不会自动更正 \pi 和 \times。否则打印等式。有人可以建议我前进的方向是什么。下面是代码
Sub AreaSolidBolt(wrdApp As Object, wrdDoc As Object, d As Variant)
Dim objRange As Object
Dim objEq As OMath
Dim aCorrect As OMathAutoCorrectEntry
wrdApp.OMathAutoCorrect.UseOutsideOMath = True
Set objRange = wrdDoc.Range
objRange.Text = "A = \pi/4 \times d^2"
Set objRange = wrdApp.Selection.OMaths.Add(objRange)
For Each aCorrect In wrdApp.OMathAutoCorrect.Entries
With objRange
If InStr(.Text, aCorrect.Name) > 0 Then
.Text = Replace(.Text, aCorrect.Name, aCorrect.Value)
End If
End With
Next aCorrect
Set objEq = objRange.OMaths(1)
objEq.BuildUp
Set objRange = Nothing
End Sub
我在调用函数中定义了如下对象。你能告诉我前进的方向吗?
Set fso = CreateObject("Scripting.FileSystemObject")
Set wrdApp = CreateObject("Word.Application")
If Not fso.FileExists(wrdFileName) Then
Set wrdDoc = wrdApp.Documents.Add
wrdApp.Visible = False
With wrdDoc
.SaveAs FileName:=wrdFileName
End With
Else
Set wrdDoc = wrdApp.Documents.Open(wrdFileName)
wrdApp.Visible = False
wrdDoc.Content.InsertAfter vbLf
End If
如果自动更正无法识别您需要使用的内容,那么您可以使用 ChrW() 写入相应的 (unicode) 字符代码作为等式的一部分。例如PI为960。
不清楚您认为 "times" 字符是什么,"x" 是“*”还是其他字符。我注意到大多数数学方程式实际上并不使用字符进行乘法运算,这可能就是自动更正功能不接受该字符的原因。但是您当然可以直接输入这些内容吗?
例如:
objRange.Text = "A = " & ChrW(960) & "/4 * d^2"
我自己找到了答案。略有修改。错误与代码行的位置有关 设置 objRange = wrdApp.Selection.OMaths.Add(objRange)
下面是修改后的代码。
Sub AreaSolidBolt(wrdApp As Object, wrdDoc As Object, d As Variant)
Dim objRange As Object
Dim objEq As OMath
Dim aCorrect As OMathAutoCorrectEntry
wrdApp.OMathAutoCorrect.UseOutsideOMath = True
Set objRange = wrdDoc.Range
objRange.Text = "A = \pi/4 \times d^2"
For Each aCorrect In wrdApp.OMathAutoCorrect.Entries
With objRange
If InStr(.Text, aCorrect.Name) > 0 Then
.Text = Replace(.Text, aCorrect.Name, aCorrect.Value)
End If
End With
Next aCorrect
Set objRange = wrdApp.Selection.OMaths.Add(objRange)
Set objEq = objRange.OMaths(1)
objEq.BuildUp
Set objRange = Nothing
End Sub