如何 select Word 中的文本并使用 vba 将其转换为堆叠分数?

How to select a text in Word and convert it to stacked fraction with vba?

我尝试selectWord中的文字,例如:

10/8
18/6
12/4

并使用宏将其转换为带有水平线的此类分数:

Sub EscribeFraccion()

    Dim objRango As Range
    Dim objEq As OMath
     
    Set objRango = Selection.Range
    objRango.Text = "9/36"
    Set objRango = Selection.OMaths.Add(objRango)
    Set objEq = objRango.OMaths(1)
    objEq.BuildUp
    
End Sub

我希望所有的文本都被 selected 并且每个项目都像这样被转换。

我有这段代码,它的作用是通过一个文本框询问你,分数:

Sub MakeFraction ()
Dim Fraction As String, Numerator As String, Denominator As String
ActiveWindow.View.ShowFieldCodes = True
With Selection
  'For user input, you could use the following 2 lines to create a fraction
   Fraction = InputBox ("Please input the Fraction (ex: 1/2, 5/32)")
   .Collapse (wdCollapseStart)
  'Alternatively, to convert a selection, use the following line
  'Fraction = Trim (.Text)
  Numerator = Split (Fraction, "/") (0)
  Denominator = Split (Fraction, "/") (1)
  .Font.Size = Round (.Font.Size) / 2
  .Fields.Add Range: = Selection.Range, Type: = wdFieldEmpty, _
    PreserveFormatting: = False, Text: = "EQ \ f (" & Numerator & "," & Denominator & ")"
  .MoveLeft wdCharacter, 2
  .Delete
  .Fields.Update
End With
ActiveWindow.View.ShowFieldCodes = False
End Sub

但我想实现的不是 imputStrFrac = Split (Selection.Text, "/") 我该怎么做?非常感谢您的支持。您好!

试试这个代码:

Sub EscribeFraccion()
    With ActiveDocument.Range.Find  'or Selection.Range.Find
        .Text = "[0-9]@/[0-9]@"
        .MatchWildcards = True
        Do While .Execute
            .Parent.OMaths.Add(.Parent).OMaths(1).BuildUp
        Loop
    End With
End Sub

编辑2

Sub EscribeFraccionSel()
    With Selection.Range.Find
        .Text = "[0-9]@/[0-9]@"
        .MatchWildcards = True
        Do While .Execute
            .Parent.Text = InputBox("Please input the Fraction (ex: 1/2, 5/32)", "Input the Fraction", .Parent.Text)
            .Parent.OMaths.Add(.Parent).OMaths(1).BuildUp
        Loop
    End With
End Sub

编辑3

Sub EscribeFraccionSel()
    Dim inp
    With Selection.Range.Find
        .Text = "[0-9]@/[0-9]@"
        .MatchWildcards = True
        Do While .Execute
            inp = Trim(InputBox("Please input the Fraction (ex: 1/2, 5/32)", "Input the Fraction", .Parent.Text))
            inp = Split(inp, "/")
            If UBound(inp) = 1 Then
                If IsNumeric(inp(0)) And IsNumeric(inp(1)) Then
                    .Parent.Text = inp(0) & "/" & inp(1)
                    .Parent.OMaths.Add(.Parent).OMaths(1).BuildUp
                Else
                    MsgBox "Error in the entered fraction"
                End If
            Else
                MsgBox "Error in the entered fraction"
            End If
        Loop
    End With
End Sub