从Word VBA中的空格中删除下标/上标

问题描述 投票:-1回答:1

我的目标是从空格中删除下标或上标格式。

当我运行下面的代码时,只有top函数有效,而lower函数无法执行任何操作。如果我切换功能的位置,仍然只有顶部功能工作。功能几乎完全相同,我不知道从哪里开始修复它。请有人指出我正确的方向吗?

Dim oRng As Word.Range
Set oRng = ActiveDocument.Range

'For subscripted spaces

Selection.HomeKey unit:=wdStory
With oRng.Find
 .ClearFormatting
 .Replacement.ClearFormatting
.Forward = True
.Text = "[A-z0-9] "
.Replacement.Text = ""
.MatchWholeWord = False
    While .Execute
        If oRng.Characters(2).Font.Subscript = True Then
        oRng.Characters(2).Font.Subscript = False
        End If
    oRng.Collapse direction:=wdCollapseEnd
    Wend
End With

'For superscripted spaces

 Selection.HomeKey unit:=wdStory
With oRng.Find
 .ClearFormatting
 .Replacement.ClearFormatting
.Forward = True
.Text = "[A-z0-9] "
.Replacement.Text = ""
.MatchWholeWord = False
    While .Execute
        If oRng.Characters(2).Font.Superscript = True Then
        oRng.Characters(2).Font.Superscript = False
        End If
    oRng.Collapse direction:=wdCollapseEnd
    Wend
End With
ms-word word-vba
1个回答
0
投票

问题是,当您完成处理首先放置的系列时,Rng定义的范围已折叠到最后一个Found实例。无论如何,你不需要循环。尝试:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = " "
    .Replacement.Text = "^&"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .Font.Superscript = True
    .Replacement.Font.Superscript = False
    .Execute Replace:=wdReplaceAll
    .Font.Subscript = True
    .Replacement.Font.Subscript = False
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.