优化查找和替换MS-Word VBA脚本 [英] Optimizing Find and Replace MS-Word VBA Script

查看:215
本文介绍了优化查找和替换MS-Word VBA脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个宏,用于替换三个空格中的第一个空格的格式,后跟一个带有蓝色字体的数字的特定字符串,另一个用于替换括号内的空格,后跟一个特定的字符串.

I wrote a macro for replacing the format of the first of three spaces followed by a certain string with a digit in blue font and another one for replacing the space inbetween brackets followed by a certain string.

您知道如何优化这两个过程吗(我使用MS-Word的搜索和替换对话框的通配符,但是猜想在VBA中使用它是很尴尬的..)

Do you know how to optimize these two procedures (I use wildcards of the search and replace dialogue of the MS-Word but guess it is rather awkard to use this in VBA..)?

我的宏:

Sub replace_3spaces()

Dim str_after As String
Dim re_number As Integer

str_after = "normal"
re_number = "1"

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "([^s]{3})" & "(" & str_after & ")"
        .Replacement.Text = "§§§\2"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.Font.ColorIndex = wdBlue
    With Selection.Find
        .Text = "§§§"
        .Replacement.Text = re_number & " "
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

推荐答案

我不确定您要执行的操作,因为您的代码确实有效.当您说优化时,您是在问是否有一种更快的方法,还是在问是否可以缩短您的代码?我看不到您在开始时设置尺寸的任何原因,因此,如果您只是在寻找较短的代码,则可以使用以下代码:

I'm not entirely sure what you're trying to do because your code does work. When you say optimize, are you asking if there's a quicker way to do it, or are you asking if your code can be shortened? I can't see any reason for the dimensions you set at the beginning, so if you're just looking for shorter code, you could use the following:

    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "([^s]{3})(normal)"
        .Replacement.Text = "§§§\2"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
        .Forward = False
        .ClearFormatting
        .Replacement.Font.ColorIndex = wdBlue
        .Format = True
        .Text = "§§§"
        .Replacement.Text = "1 "
        .Execute Replace:=wdReplaceAll
    End With

基于您具有这些尺寸的事实,并且您对其中一个尺寸使用了数字,我忍不住认为您实际上是在尝试创建一个编号系统,其中为每个实例赋予一个编号.如果是这样,那么您将使用以下代码:

Based on the fact that you have those dimensions, and you used a number for one of them, I can't help but think that you were actually trying to create a numbering system, where each instance is given a number. If that's the case, here's the code you would use:

Dim str_after, oldColor As String
Dim re_number As Integer

str_after = "normal"
re_number = "1"

    Selection.HomeKey unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "([^s]{3})" & "(" & str_after & ")"
        .Replacement.Text = "§§§\2"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
        While Selection.Find.Execute
            oldColor = Selection.Font.Color
            Selection.Font.Color = wdColorBlue
            Selection.TypeText Text:=re_number & " "
            Selection.Font.Color = oldColor
            Selection.TypeText Text:=str_after
            re_number = re_number + 1
        Wend

这篇关于优化查找和替换MS-Word VBA脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆