2012-11-14 64 views
1

在替換字符串我試圖創建一個VB腳本,將Word文檔中的某些字符替換,我在下面的方式進行管理:的VBScript多查找/ 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 

然而,我設法做的方式非常緩慢,尤其是當文本文件爲幾MB時需要更換的字符數量增加時......因此,腳本會遍歷整個文檔中的每個替換字符,導致腳本執行速度非常慢。是否有可能在一個word文檔中平行更改幾個字符,只有一個腳本通過文檔並根據遇到哪個字符執行替換? 我也嘗試從word文檔逐行加載到一個變量和比較每個字符與給定的字符替換,導致腳本執行速度慢得多...對不起,我希望我成功地解釋我的問題。 請幫幫我。謝謝。 :)

回答

0

你能做這樣的事情嗎?

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 

我覺得如果你查找並替換單個字符,但這樣做是符合其中任何一個它的發現,並與相應的字符替換它,你正在尋找的字符,檢查這可能只是工作。

這是從Word 2010中記錄的宏取得的,但我不認爲應該有必要轉換爲VBScript。

+0

非常感謝您的回覆,但這是比較慢的方式... – nexry