Hi,
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.
My Code:
Sub OpenDocuments()
Dim wrd1App As Word.Application
Dim target As Word.Document
On Error GoTo Exit_Proc
Set wrd1App = CreateObject("Word.Application")
Set target = wrd1App.Documents.Open("C:\Documents and Settings\user\My Documents\target.dotx")
Call SetProtection(target)
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
Sub FindAndReplaceFirstStoryOfEachType(source As Word.Document, target As Word.Document)
Dim newStr As String
newStr = "NEW STRING"
Dim rngStory As Range
Set rngStory = target.Range
Call repII("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
Sub repII(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
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)
If doc.ProtectionType <> wdNoProtection Then
doc.Unprotect Password:="12345"
End If
End Sub
Please, could anyone explain me why rngStory.Text = newStr returns the error 6124 and why procedures repII and repIII do not make any text replacement in target MS Word document.
Thank you very much for your advice.
Tomas
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.
My Code:
Sub OpenDocuments()
Dim wrd1App As Word.Application
Dim target As Word.Document
On Error GoTo Exit_Proc
Set wrd1App = CreateObject("Word.Application")
Set target = wrd1App.Documents.Open("C:\Documents and Settings\user\My Documents\target.dotx")
Call SetProtection(target)
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
Sub FindAndReplaceFirstStoryOfEachType(source As Word.Document, target As Word.Document)
Dim newStr As String
newStr = "NEW STRING"
Dim rngStory As Range
Set rngStory = target.Range
Call repII("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
Sub repII(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
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)
If doc.ProtectionType <> wdNoProtection Then
doc.Unprotect Password:="12345"
End If
End Sub
Please, could anyone explain me why rngStory.Text = newStr returns the error 6124 and why procedures repII and repIII do not make any text replacement in target MS Word document.
Thank you very much for your advice.
Tomas