简体   繁体   English

vbscript在Word文档中多查找/替换字符串

[英]vbscript multi find/replace string in word document

I'm trying to create a VB script that will perform the substitution of certain characters in a word document and I managed in the following way: 我正在尝试创建一个VB脚本,该脚本将执行Word文档中某些字符的替换,并且通过以下方式进行管理:

objSelection.Find.Text = "["
objSelection.Find.Forward = TRUE
objSelection.Find.Replacement.Text = "q"
objSelection.Find.Execute ,,,,,,,,,,2
objSelection.Find.Text = "{"
objSelection.Find.Forward = TRUE
objSelection.Find.Replacement.Text = "w"
objSelection.Find.Execute ,,,,,,,,,,2
objSelection.Find.Text = "^"
objSelection.Find.Forward = TRUE
objSelection.Find.Replacement.Text = "y"
objSelection.Find.Execute ,,,,,,,,,,2
objSelection.Find.Text = "~"
objSelection.Find.Forward = TRUE
objSelection.Find.Replacement.Text = "z"
objSelection.Find.Execute ,,,,,,,,,,2
objSelection.Find.Text = "]"
objSelection.Find.Forward = TRUE
objSelection.Find.Replacement.Text = "x"
objSelection.Find.Execute ,,,,,,,,,,2
objSelection.Find.Text = "}"
objSelection.Find.Forward = TRUE
objSelection.Find.Replacement.Text = "ć"
objSelection.Find.Execute ,,,,,,,,,,2
objSelection.Find.Text = "@"
objSelection.Find.Forward = TRUE
objSelection.Find.Replacement.Text = "]"
objSelection.Find.Execute ,,,,,,,,,,2

However, the way in which I managed to do is a very slow, especially when increasing the number of characters that need to be replaced when a text file is several MB... So, the script goes through the entire documet for each replace characters and that resulting is very slow execution of script. 但是,我设法完成的方法非常慢,尤其是当文本文件为几MB时增加需要替换的字符数时...因此,脚本会遍历每个替换字符的整个文档结果导致脚本执行非常缓慢。 Is there a possibility of parallel changes several characters in a word document, the script only one pass through the document and depending on which character is encountered, to perform the replacement? 是否有可能并行更改一个单词文档中的几个字符,而脚本仅一个字符遍历文档,并根据遇到的字符来进行替换? I also tried loading line by line from word document into a variable and comparison of each character with the given character for a replacement which resulted in much slower execution of scripts... Sorry for my english, i hope i succeeded to explain my problem. 我还尝试了将单词文档中的每一行逐行加载到变量中,并将每个字符与给定字符进行比较以进行替换,这会导致脚本的执行速度大大降低...对不起,我的英文,希望我能成功解释我的问题。 Please help me. 请帮我。 Thank you. 谢谢。 :) :)

Could you do something like this? 你能做这样的事情吗?

objSelection.Find.ClearFormatting
With objSelection.Find
    .Text = "[\[\{~\]\}\@^0094]" ' ^0094 is "^"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = 1 'wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
End With

Do While objSelection.Find.Execute()
    Dim t: t = objSelection.Text
    Select Case t
        Case "["
            objSelection.Text = "q"
        Case "{"
            objSelection.Text = "w"
        Case "~"
            objSelection.Text = "z"
        '... fill in the rest
    End Select
    objSelection.Move 1
Loop

I think this may only work if you look for and replace single characters, but what this does is match any of the characters you are looking for, checks which one it's found and replaces it with the appropriate character. 我认为这可能仅在您查找并替换单个字符时才有效,但是这样做是与您要查找的任何字符匹配,检查找到的哪个字符,然后将其替换为适当的字符。

This was taken from a recorded macro in Word 2010 but I don't think any conversion to VBScript should be necessary. 这是从Word 2010中记录的宏中获取的,但我认为没有必要将其转换为VBScript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM