标记之间的格式编号为下标

时间:2014-09-02 07:22:39

标签: vba replace word-vba text-formatting

我想在两个主题标签之间找到一个数字,删除主题标签并将主题标签之间的数字格式化为下标。

示例:文本##12##应转换为 12

到目前为止,这是我的代码:

Public Sub SubscriptText()
    With Selection.Find
         .ClearFormatting
         .Text = "##"
         .Replacement.ClearFormatting
         .Replacement.Text = "##"
         .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
    End With
End Sub

这会删除##,但如何将数字格式化为下标?

1 个答案:

答案 0 :(得分:2)

我会这样做:查找开始标签的结尾,以及结束标签的开头。这两个位置之间的所有内容可能都是您想要更改字体的数字。

end of
opening
hashtag
    ꜜ
 # # 1 2 # # 
        ꜛ
     start of
     closing
     hashtag       

这对我有用:

Dim wd As Document
Dim rOpeningHashtag As Range
Dim rClosingHashtag As Range
Dim rBewteenHashtags As Range
Dim hashtagFound As Boolean

Set wd = ActiveDocument
wd.Range.Select

Do
    'Look for opening hashtag
    hashtagFound = Selection.Find.Execute("##")
    If Not hashtagFound Then Exit Do 'end of document reached
    Set rOpeningHashtag = Selection.Range
    'Look for closing hashtag
    Selection.Find.Execute "##"
    Set rClosingHashtag = Selection.Range

    'Number is in between the two.
    Set rBewteenHashtags = wd.Range(rOpeningHashtag.End, rClosingHashtag.Start)
    rBewteenHashtags.Font.Subscript = True
    'Get rid of the opening and closing hashtags
    rClosingHashtag.Delete
    rOpeningHashtag.Delete
Loop