【问题标题】:Format number between markers as subscript将标记之间的数字格式化为下标
【发布时间】:2014-10-26 09:30:00
【问题描述】:

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

示例:文本##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

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

【问题讨论】:

标签: vba replace ms-word text-formatting


【解决方案1】:

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

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

【讨论】:

  • 非常有用,谢谢。我需要在两次搜索之间使用Selection.Collapse wdCollapseEnd,否则第二次搜索只会在第一次搜索的结果中搜索。
猜你喜欢
  • 1970-01-01
  • 2014-12-28
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
  • 2021-12-30
  • 1970-01-01
相关资源
最近更新 更多