質問: 複数のwordファイルを検索して置換するにはどうすればよいですか? 多くのword(.doc)ファイルで検索と置換を行う方法を探しています(プロセスの自動化を目的としています)。これまでに見つけたソフトウェアでは、検索のみが可能で、置き換えはできません。wordでそれを行うにはどうすればよいですか?
回答: 指定されたルートフォルダーの下の複数のフォルダーにある複数のmswordファイルで複数の通常およびワイルドカードベースの置換を実行する目的で、次のvbaマクロを作成しました。これを使用するには、次の変数(定数)の内容を変更します。 rootpath:word文書を含むフォルダーが置かれているルートフォルダー。 findtextswildおよびreplacetextswild:ワイルドカードベースの検索および置換式の配列。 findtextsおよびreplacetexts:通常の検索および置換式の配列。 あなたはそれが便利だと思うかもしれません:-)sub globaltextreplacement() ' root under which all manuals are stored dim rootpath as string rootpath = "c:\data\manuals\" ' find and replace text for wildcard replacement. performed first. dim findtextswild() as variant, replacetextswild() as variant findtextswild = array("[ ]{2;}", "[cc]onfiguration[/ ]@[pp]olicy [rr]epository", "[ss]ervlet[- ]@[ff]ilter") replacetextswild = array(" ", "configuration/policy repository", "servlet-filter") ' find and replace text for normal case insensitive replacement. performed second. dim findtexts() as variant, replacetexts() as variant findtexts = array("dirx access", "policy repository", "user repository", "servlet", "servletfilter", "saml assertion", "dirx access server", "dirx access manager", "deployment manager", "policy manager", "client sdk", "^p ", " ^p") replacetexts = array("dirx access", "policy repository", "user repository", "servlet", "servlet-filter", "saml assertion", "dirx access server", "dirx access manager", "deployment manager", "policy manager", "client sdk", "^p", "^p") ' main code application.screenupdating = false dim dirnames(20) as string dim dirnamescount as integer dirnamescount = 0 dim dirname as string dirname = dir$(rootpath & "*", vbdirectory) do until lenb(dirname) = 0 dim dirpath as string dirpath = rootpath & dirname if ((getattr(dirpath) and vbdirectory) = vbdirectory) and (dirname <> ".") and (dirname <> "..") then dirnamescount = dirnamescount + 1 dirnames(dirnamescount) = dirpath & "\" end if dirname = dir$ loop do while dirnamescount > 0 dim filename as string dirname = dirnames(dirnamescount) dirnamescount = dirnamescount - 1 filename = dir$(dirname & "*.doc", vbdirectory) do until lenb(filename) = 0 dim filepath as string filepath = dirname & filename filename = dir$ dim document as document set document = documents.open(filepath) document.trackrevisions = true document.select dim i as integer, maxindex as integer maxindex = ubound(findtextswild) for i = lbound(findtextswild) to maxindex with selection.find .clearformatting .replacement.clearformatting .text = findtextswild(i) .replacement.text = replacetextswild(i) .execute replace:=wdreplaceall, forward:=true, _ wrap:=wdfindcontinue, matchwildcards:=true end with next maxindex = ubound(findtexts) for i = lbound(findtexts) to maxindex with selection.find .clearformatting .replacement.clearformatting .text = findtexts(i) .replacement.text = replacetexts(i) .execute replace:=wdreplaceall, forward:=true, _ wrap:=wdfindcontinue, matchcase:=false, matchwildcards:=false end with next document.save document.close loop loop application.screenupdating = true end sub