【问题标题】:VBA to insert before and after superscript and subscript in MSWordVBA在MSWord中的上标和下标之前和之后插入
【发布时间】:2019-02-03 14:54:11
【问题描述】:

我正在尝试创建 VBA 以在 Supercript 和下标之前和之后插入。我的代码如下。

Public Sub MySubscriptSuperscript() 
Dim myRange As Word.Range, myChr 
For Each myRange In ActiveDocument.StoryRanges  
Do     
     For Each myChr In myRange.Characters 
     If myChr.Font.Superscript = True Then
        myChr.Font.Superscript = False
        myChr.InsertBefore "<sup>"
        myChr.InsertAfter "</sup>"
    End If  

    If myChr.Font.Subscript = True Then
        myChr.Font.Subscript = False
        myChr.InsertBefore "<sub>"
        myChr.InsertAfter "</sub>"
    End If
Next
Set myRange = myRange.NextStoryRange
Loop Until myRange Is Nothing   
Next 
End Sub

此代码适用于上标和下标的每个字符。

但是,我正在寻找在完整的上标/下标单词/字母之前和之后插入标签的 VBA。

例子

C12H22O11 和 x23 + y39 7 + x67

VBA 上面给出以下输出

C<sub>1</sub><sub>2</sub>H<sub>2</sub><sub>2</sub>O<sub>1</sub><sub>1</sub><sub> </sub><sub> </sub> and x<sup>2</sup><sup>3</sup> + y<sup>3</sup><sup>9</sup><sup>7</sup> + x<sup>6</sup><sup>7</sup>

但我正在寻找这个输出

C<sub>12</sub>H<sub>22</sub>O<sub>11</sub> and x<sup>23</sup> + y<sup>397</sup> + x<sup>67</sup>

请指导,如何实现。

【问题讨论】:

    标签: vba ms-word subscript superscript


    【解决方案1】:

    我很想采用最简单的方法来获得最终结果 - 最后,只需将 &lt;/sub&gt;&lt;sub&gt;&lt;/sup&gt;&lt;sup&gt; 替换为空字符串 ""

    但是,我这样懒惰......

    编辑 - 只是一个想法: 用替换来完成整个事情不是更快吗?您不必检查每个字符。以下是 Word 为替换记录的内容,需要稍加打磨:

        Selection.Find.Replacement.ClearFormatting
        With Selection.Find.Replacement.Font
            .Superscript = False
            .Subscript = False
        End With
        With Selection.Find
            .Text = "^?"
            .Replacement.Text = "<sup>^&</sup>"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    

    因此,最后,您将运行 search&replace 4 次:

    • 替换上标
    • 删除上标的结束标签和开始标签
    • 替换下标
    • 删除下标的结束标签和开始标签

    【讨论】:

    • 明确一点,我的意思是用vba来做,这样你就可以用宏得到你想要的结果。
    【解决方案2】:

    试试:

    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Forward = True
        .Text = ""
        .Wrap = wdFindContinue
        .Font.Subscript = True
        .Replacement.Text = "<sub>^&<\sub>"
        .Execute Replace:=wdReplaceAll
        .Font.Superscript = True
        .Replacement.Text = "<sup>^&<\sup>"
        .Execute Replace:=wdReplaceAll
      End With
    End With
    Application.ScreenUpdating = True
    End Sub
    

    不明白为什么要循环遍历所有故事范围,因为这些内容通常只会出现在文档正文中。也就是说,修改代码以适用于所有故事范围很容易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-07
      • 2017-04-18
      • 1970-01-01
      相关资源
      最近更新 更多