如果选择只有 1 个搜索字符实例,VBA 宏不起作用

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

我正在编写一个宏,将选定的星号字符转换为上标星号。选择很重要,因为我不想完全搞乱我的文档。这是代码:

Sub Superstarv4()
' makes all selected asterisks superscripted
    Dim selRange As Range

    ' Check if text is selected
    If Selection.Type = wdSelectionIP Then
        MsgBox "No text selected. Please select some text.", vbExclamation
        Exit Sub
    End If

    ' Set selRange to the selected range
    Set selRange = Selection.Range

    ' Apply formatting only to the selected range
    With selRange.Find
        .ClearFormatting
        .Text = "*"  ' Find asterisks
        .Replacement.ClearFormatting
        .Replacement.Font.Superscript = True  ' Format as superscript
        .Execute Replace:=wdReplaceAll
    End With
End Sub

这大部分都有效。我的目标是将整个过程变成按下按钮。但有一个问题。如果我选择一 (1) 个星号字符,此代码将工作错误,它会转换整个文档中上标的以下所有星号字符。此问题包含所有 VBA 替换操作。我有一个类似的代码,可以将逗号变成点,也有同样的问题。

有一个快速解决问题的方法,如下所示:

Sub Superstarv5()
    ' Check if text is selected
    If Selection.Type = wdSelectionIP Then
        MsgBox "No text selected. Please select some text.", vbExclamation
        Exit Sub
    End If

    Dim selRange As Range
    Set selRange = Selection.Range

    ' Ensure the range is valid and has text
    If selRange.Text = "" Then
        MsgBox "Selected text is empty. Please select some text.", vbExclamation
        Exit Sub
    End If

    ' Count the number of asterisks in the selected text
    Dim asteriskCount As Long
    asteriskCount = Len(selRange.Text) - Len(Replace(selRange.Text, "*", ""))

    ' Check if there is more than one asterisk
    If asteriskCount <= 1 Then
        MsgBox "Selected text does not contain more than one asterisk. No formatting applied.", vbInformation
        Exit Sub
    End If

    ' Apply superscript formatting only to the selected range
    With selRange.Find
        .ClearFormatting
        .Text = "*"  ' Find asterisks
        .Replacement.ClearFormatting
        .Replacement.Font.Superscript = True  ' Format as superscript
        .Execute Replace:=wdReplaceAll
    End With
End Sub

这首先确保用户选择了两个星号实例。但我不想要变通办法或快速修复。我做错了什么或者这是 MSWord 中已知(或未知)的错误吗?

vba replace ms-word
1个回答
0
投票

原始代码中的主要问题是,当仅选择一个星号时,查找操作会影响整个文档。通过添加 .Wrap = wdFindStop,搜索仅限于所选范围,从而防止在选择范围之外进行更改。这可确保宏上标仅显示选定的星号,无论存在多少个星号。

调整后的关键部分如下:

.Wrap = wdFindStop ' Stop at the end of the selection
Sub SuperstarFixed()
    Dim selRange As Range

    ' Check if text is selected
    If Selection.Type = wdSelectionIP Then
        MsgBox "No text selected. Please select some text.", vbExclamation
        Exit Sub
    End If

    ' Set selRange to the selected range
    Set selRange = Selection.Range

    ' Apply formatting only to the selected range
    With selRange.Find
        .ClearFormatting
        .Text = "*"  ' Find asterisks
        .Replacement.ClearFormatting
        .Replacement.Font.Superscript = True  ' Format as superscript
        .Wrap = wdFindStop ' Stop at the end of the selection
        .Execute Replace:=wdReplaceAll
    End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.