找到文本的更好方法,选择段落开始,然后重新格式化

发布于 2025-01-26 10:36:54 字数 1351 浏览 5 评论 0原文

我正在写一个宏来找到一个开放的括号,从该字符开始到段落的开始,然后将所选文本重新格式化为小帽子。公平的警告,我对VBA一无所知,我从网上找到的示例中将这些宏删除在一起。

宏的最后一部分将光标移至下一段的开始,以便我可以再次运行。我想循环,但是我还没有想出如何为循环设置停止点。

以下宏有效,但是必须有一种更好的方法来做到这一点。此方法会产生大量的屏幕闪烁,并永远需要运行。

Sub References()
'
' References Macro

'this part selects text until it finds an open parenthesis
Dim flag As Boolean
flag = True
While flag = True
    Selection.MoveRight Unit:=wdCharacter, Count:=1, _
    Extend:=wdExtend
    'checks the last character to see if its a open parenthesis
    If Strings.Right(Selection.Range.Text, 1) = "(" Then
        'if it was an open parenthesis the loop flag will end
        flag = False
    End If
Wend

'this part reformats the text
    With Selection.Font
        .Name = "Times New Roman"
        .Size = 12
        .Bold = False
        .Italic = False
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .StrikeThrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .SmallCaps = True
        .AllCaps = False
        .Color = wdColorAutomatic
        .Spacing = 0
        .Scaling = 100
        .Position = 0
        .Kerning = 1
    End With

'this part moves the cursor to the beginning of the next paragraph
    Selection.HomeKey Unit:=wdLine
    Selection.MoveDown Unit:=wdParagraph, Count:=1

End Sub

I'm writing a macro in Word to find an open parenthesis, select from that character to the start of the paragraph, and then reformat the selected text into small caps. Fair warning, I know nothing about VBA and I hack these macros together from examples I find online.

The last part of the macro moves the cursor to the start of the next paragraph so that I can run it again. I'd like to loop it, but I haven't figured out how to set a stopping point for the loop yet.

The following macro works, but there has to be a better way to do it. This method results in a lot of screen flicker and takes forever to run.

Sub References()
'
' References Macro

'this part selects text until it finds an open parenthesis
Dim flag As Boolean
flag = True
While flag = True
    Selection.MoveRight Unit:=wdCharacter, Count:=1, _
    Extend:=wdExtend
    'checks the last character to see if its a open parenthesis
    If Strings.Right(Selection.Range.Text, 1) = "(" Then
        'if it was an open parenthesis the loop flag will end
        flag = False
    End If
Wend

'this part reformats the text
    With Selection.Font
        .Name = "Times New Roman"
        .Size = 12
        .Bold = False
        .Italic = False
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .StrikeThrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .SmallCaps = True
        .AllCaps = False
        .Color = wdColorAutomatic
        .Spacing = 0
        .Scaling = 100
        .Position = 0
        .Kerning = 1
    End With

'this part moves the cursor to the beginning of the next paragraph
    Selection.HomeKey Unit:=wdLine
    Selection.MoveDown Unit:=wdParagraph, Count:=1

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聚集的泪 2025-02-02 10:36:54

例如,使用查找(比测试每个字符更快):

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "("
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = False
  End With
  Do While .Find.Execute
    i = i + 1
    .Start = .Paragraphs.First.Range.Start
    .Font.SmallCaps = True
    .Start = .Paragraphs.First.Range.End
  Loop
End With
Application.ScreenUpdating = True
MsgBox i & " instances processed."
End Sub

For example, using Find (which is way faster than testing every character):

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "("
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = False
  End With
  Do While .Find.Execute
    i = i + 1
    .Start = .Paragraphs.First.Range.Start
    .Font.SmallCaps = True
    .Start = .Paragraphs.First.Range.End
  Loop
End With
Application.ScreenUpdating = True
MsgBox i & " instances processed."
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文