VBA Word查找和替换字符但不包括某些字符 [英] VBA Word Find and Replace characters but excluding certain characters

查看:593
本文介绍了VBA Word查找和替换字符但不包括某些字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Word中编写VBA代码,最终将从VBA Excel模块运行。该项目的目的是在打开的Word文档中查找长度为一个或两个字符且具有特定格式的特定字符串,并将
替换为具有相同格式的其他字符串。这与在Word中的歌曲表中的和弦符号的转置(即,改变音乐键)有关。查找和替换字符串包含在Excel工作簿的范围中,这就是为什么我想最终
从Excel运行代码。我在Excel中编写VBA代码比在Word中编写VBA代码更有经验,而且我非常有信心将"Word VBA"代码转换为Excel模块。

I am trying to write VBA code in Word that I will eventually run from a VBA Excel module. The aim of the project is to find specific strings in the open Word document that have length of either one or two characters and are of a certain format, and replace them with other strings in the same format. This is to do with transposing (i.e. changing the musical key) of chord symbols in a songsheet in Word. The Find and Replace strings are contained in ranges in an Excel workbook, which is why I want to eventually run the code from Excel. I'm much more experienced in writing VBA code in Excel than in Word, and I'm fairly confident with transferring the 'Word VBA' code into an Excel module.

目前我我正在用Word完全尝试代码,而且我遇到了一个绊脚石。例如,我希望它能够找到" A "并且只有在" A "时才会替换为" B ",
后跟"" (尖锐的)或" b " (flat)。

At the moment I'm trying out code entirely in Word, and I've come across a stumbling block. For example, I want it to Find "A" and replace with "B", BUT only if the "A" is NOT followed by "#" (sharp) or "b" (flat).

这是我在Word VBA中获得的代码,我通过编辑记录器生成的代码获得:

Here is the code I've got in Word VBA, which I obtained by editing code produced by the recorder:

Sub F_R()

    'Find text must have specific font
    With Selection.Find.Font
        .Bold = True
        .Underline = wdUnderlineWords
        .Superscript = False
        .Subscript = False
    End With
    
    'Replacement text must have specific font
    With Selection.Find.Replacement.Font
        .Bold = True
        .Underline = wdUnderlineWords
        .Superscript = False
        .Subscript = False
    End With
    
    'Find & Replace strings
    With Selection.Find
        .Text = "A" 'hard-coded here for testing, but this will
                    'eventually be referenced to a cell in Excel
        .Replacement.Text = "B" 'hard-coded here for testing, but this will
                                'eventually be referenced to a cell in Excel
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = True
        .MatchWholeWord = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    
End Sub

对于Find&替换部分我想做类似的事情:

For the Find & Replace section I want to do something like:

With Selection.Find .Text = "A" .Text <> "A#" .Text <> "Ab" .Replacement.Text = "B" End With

- 但这会产生语法错误,大概是因为你只能有一条.Text行(或者它不会接受< ;>?)

- but this produces a syntax error, presumably because you can have only one .Text line (or it won't accept <>?)

我尝试在使用Like运算符时采用排除字符的方式,并且在编译时,它不会替换
任何 " A":

I tried adopting the way of excluding chars when using the Like operator, and while it compiles, it will not replace any "A":

    With Selection.Find
        .Text = "A[!b#]"
        .Replacement.Text = "B"
    End With

我怀疑我必须完全按照我这样做的方式改变方向。请问您有什么建议吗?

I suspect that I'm going to have to change tack completely in the way I'm doing this. Do you have any suggestions, please?

和弦名称/符号在空格或段落返回之前/之后,可以看起来像这些,例如(仅限所有字体粗体和带下划线的单词) ):

The chord names/symbols are preceded/succeeded by either spaces or paragraph returns and can look like these, for example (all Font Bold and Underlined words only):

C< sup> 7< / sup>

Dm< sup> 7< / sup>

Eb< sup> -5< / sup>

Bb< sup> + 11< / sup>

F#m< sup> 7< / sup>

即[ ABCDEFG ] [ b# |可选] [ m |可选] [ - + |上标,可选] [ 2 3
5 6 7 9 11 13 |上标,可选]

i.e. [ABCDEFG][b # | optional][m |optional][- + | superscript, optional][2 3 5 6 7 9 11 13 | superscript, optional]

我的问题的症结在于,笔记A应该被视为完全不同于Ab或A#(和类似的对于其他扁平/锐化的音符)。

The crux of my problem is that the note A should be treated as entirely distinct from Ab or A# (and similar for other flattened/sharpened notes).

很抱歉很长的帖子。

推荐答案

我担心这个论坛只支持Visual Studio中的Visual Basic(VB.Net)。如果你在

Word for Developers论坛
I'm afraid this forum only supports Visual Basic in Visual Studio (VB.Net). For help with VBA in Word, you have a better chance of getting an answer if you post in the Word for Developers forum.


这篇关于VBA Word查找和替换字符但不包括某些字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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