Format number between markers as subscript

笑着哭i 提交于 2019-12-25 03:52:36

问题


I would like to find a number between two hashtags, delete the hashtags and format the number that was between the hashtags as subscript.

Example: The text ##12## should be converted to 12.

Here is my code so far:

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

This removes the ##, but how do I format the number as subscript?


回答1:


I would do this: look for the end of the opening hashtag, and the start of the closing hashtag. Everything between those two positions is presumably a number for which you want to change the font.

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

This worked for me:

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


来源:https://stackoverflow.com/questions/25617920/format-number-between-markers-as-subscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!