MS Word - 宏 - 文本替换 [英] MS Word - macro - text replacement
问题描述
我必须创建一个宏来用文本替换MS Word文档中的文本。
目标位置新文本由使用的样式定义,所以我只知道样式的名称和新文本。
I have to create a macro for replacing a text in a MS word document by a text.
The target position of new text is defined by used style, so I just know the name of the style and the new text.
我的代码:
Sub OpenDocuments( )
Dim wrd1App As Word.Application
昏暗的目标As Word.Document
On Error GoTo Exit_Proc
设置wrd1App = CreateObject(" Word.Application")
Sub OpenDocuments()
Dim wrd1App As Word.Application
Dim target As Word.Document
On Error GoTo Exit_Proc
Set wrd1App = CreateObject("Word.Application")
设置target = wrd1App.Documents.Open(" C:\Documents and Settings \user\My Documents\ target.dotx")
致电SetProtection(目标)
Set target = wrd1App.Documents.Open("C:\Documents and Settings\user\My Documents\target.dotx")
Call SetProtection(target)
On Error GoTo Exit_Proc
致电FindAndReplaceFirstStoryOfEachType(目标)
Exit_Proc:
wrd1App.Quit False
设置wrd1App = Nothing
wrd2App.Quit False
设置wrd2App = Nothing
MsgBox"出现意外错误。类型:" &安培; Err.Description& Err.Number
结束子
On Error GoTo Exit_Proc
Call FindAndReplaceFirstStoryOfEachType(target)
Exit_Proc:
wrd1App.Quit False
Set wrd1App = Nothing
wrd2App.Quit False
Set wrd2App = Nothing
MsgBox "Unexpected error. Type: " & Err.Description & Err.Number
End Sub
 
子FindAndReplaceFirstStoryOfEachType(源作为Word.Document,目标作为Word.Document)
Sub FindAndReplaceFirstStoryOfEachType(source As Word.Document, target As Word.Document)
Dim newStr As String
newStr =" NEW STRING"
Dim rngStory As Range
设置rngStory = target.Range
调用replII("候选名称",newStr,rngStory)'此程序不进行任何文本替换!!!!
使用rngStory.Find
.Replacement.Text = newStr
.Wrap = wdFindStop
.Forward = True
.Style = target.Styles(" Candidate name")
。执行'替换:= wdReplaceAll
结束与$
致电repIII("候选人名称","newStr,rngStory)"此程序不进行任何文字替换!!!!
rngStory.Text = newStr'我收到错误:6124:您不能编辑此选项,因为它受到保护
Dim testStr As String
testStr = rngStory.Text
结束子
Dim newStr As String
newStr = "NEW STRING"
Dim rngStory As Range
Set rngStory = target.Range
Call replII("Candidate name", newStr, rngStory) 'This procedure does not make any text replacement!!!!
With rngStory.Find
.Replacement.Text = newStr
.Wrap = wdFindStop
.Forward = True
.Style = target.Styles("Candidate name")
.Execute 'Replace:=wdReplaceAll
End With
Call repIII("Candidate name", newStr, rngStory) 'This procedure does not make any text replacement!!!!
rngStory.Text = newStr 'Here I get error: 6124: you are not allowed to edit this selection because it is protected
Dim testStr As String
testStr = rngStory.Text
End Sub
子replII(ByVal sFindStyle As String, ByVal sReplaceText As String,ByRef rangeDocument As Range)
使用rangeDocument.Find
.ClearFormatting
.Style = sFindStyle
.Replacement.ClearFormatting
.Replacement.Text = sReplaceText
.Execute Replace:= wdReplaceAll,Forward:= True,_
换行:= wdFindContinue
结束与$
结束子
Sub replII(ByVal sFindStyle As String, ByVal sReplaceText As String, ByRef rangeDocument As Range)
With rangeDocument.Find
.ClearFormatting
.Style = sFindStyle
.Replacement.ClearFormatting
.Replacement.Text = sReplaceText
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
End Sub
子repIII(BYVAL sFindStyle作为字符串,BYVAL sReplaceText作为字符串,为ByRef rangeDocument作为范围)
 &NBSP ; 使用rangeDocument
。选择
Selection.Collapse wdCollapseStart
Selection.TypeText sReplaceText '--CAPSID是一个存储
的数组。结束与$
结束子
Sub repIII(ByVal sFindStyle As String, ByVal sReplaceText As String, ByRef rangeDocument As Range)
With rangeDocument
.Select
Selection.Collapse wdCollapseStart
Selection.TypeText sReplaceText '--CAPSID is an array that stores
End With
End Sub
Sub SetProtection(ByRef doc As Document)
;&NBSP;如果doc.ProtectionType<> wdNoProtection然后是
doc.Unprotect密码:=" 12345"
结束如果
结束子
Sub SetProtection(ByRef doc As Document)
If doc.ProtectionType <> wdNoProtection Then
doc.Unprotect Password:="12345"
End If
End Sub
请问,有人可以解释为什么rngStory.Text = newStr返回错误6124以及为什么程序replII和repIII不会在目标MS Word文档中进行任何文本替换。
Please, could anyone explain me why rngStory.Text = newStr returns the error 6124 and why procedures replII and repIII do not make any text replacement in target MS Word document.
非常感谢您的建议。
Tomas
推荐答案
嗨Tomas,
hi Tomas,
首先要做的事情!
你的'开放文档格式' 子&NBSP;具有:
呼叫FindAndReplaceFirstStoryOfEachType(目标)
但 'FindAndReplaceFirstStoryOfEachType' 子需要两个参数 - 源和目标。因此,代码将始终抛出错误。
Your 'OpenDocuments' sub has:
Call FindAndReplaceFirstStoryOfEachType(target)
but the 'FindAndReplaceFirstStoryOfEachType' sub requires two parameters - source and target. Hence the code will always throw an error.
执行查找/替换时,通常最好设置:
.ClearFormatting
.Replacement.ClearFormatting
,以便其他操作不会格式化,如果您尝试查找特定格式,则设置:
.Format = True
同样,当你尝试寻找一种风格,但你不关心文字时,你应该有:
.Text =""
When executing a Find/Replace, it is generally good practice to set:
.ClearFormatting
.Replacement.ClearFormatting
so that no formatting carries over from other operations and, if you're trying to Find specific formats, to set:
.Format = True
Similarly, when you're trying to Find a Style, but you don't care about the text, you should have:
.Text = ""
您处理查找/替换的方式也不一致。在一个地方你有:
.Wrap = wdFindStop
.Forward = True
。执行'替换:= wdReplaceAll
但在另一个地方你有:
.Execute替换:= wdReplaceAll,Forward:= True,Wrap:= wdFindContinue
两种布局同样有效,但如果你保持一致,你的代码会更容易理解。
You're also inconsistent in the way you're processing the Find/Replace. In one place you have:
.Wrap = wdFindStop
.Forward = True
.Execute 'Replace:=wdReplaceAll
but in another place you have:
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
Both layouts are equally valid, but your code would be easier to follow if you were consistent.
当您使用'Call replII(" Candidate name",newStr,rngStory)'时,您传递了一个范围引用,但您没有告诉'replII'子文件要处理哪个文档。这可能会产生不可预测的结果。事实上,我甚至不知道为什么你需要一个单独的sub
- 它可以全部包含在'FindAndReplaceFirstStoryOfEachType'子中。
When you use 'Call replII("Candidate name", newStr, rngStory)' you're passing a range reference, but you're not telling the 'replII' sub which document to process. This can have unpredictable results. In fact, I can't even see why you need a separate sub for this - it could all be included in the 'FindAndReplaceFirstStoryOfEachType' sub.
至于 ' repIII"子,你向它传递参数,你不使用,整个子可以被替代:
rangeDocument.InsertBefore
中newstr
在‘FindAndReplaceFirstStoryOfEachType’ sub。
As for the 'repIII' sub, you pass parameters to it that you don't use, and the whole sub could be replaced by:
rangeDocument.InsertBefore newStr
in the 'FindAndReplaceFirstStoryOfEachType' sub.
这篇关于MS Word - 宏 - 文本替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!