-
[vba]Option Explicit
Function ReplaceWord(inDoc As Document, j As Long) As String
Dim wrdPara As Paragraph
Dim wrdRef As String
wrdPara = inDoc.Paragraphs(j)
wrdRef = wrdPara.Range.Text
' remove the paragraph mark:
wrdRef = Left(wrdRef, Len(wrdRef) - 1)
ReplaceWord = wrdRef
End Function
Sub Anonymouse()
Dim docCurrent As Document
Dim docRef As Document
Dim j As Long
Dim r As Range
Dim wrdRef As String
Set docCurrent = ActiveDocument
Set docRef = Documents.Open("d:\checklist.doc")
For j = 1 To docRef.Paragraphs.Count
Set r = docCurrent.Range
' gets the next word from reference doc
wrdRef = ReplaceWord(docRef, j)
With r.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Font.Color = wdColorRed
.Replacement.Text = "[REMOVED]"
.MatchWholeWord = True
.MatchCase = True
Do While .Execute(Findtext:=wrdRef, _
Forward:=True) = True
If Asc(r.Text) > 32 Then
r.Text = wrdRef
r.collapse 0
Loop
End With
Next
docRef.Close
Set docRef = Nothing
End Sub
[/vba]You do not need to activate anything. You are setting the reference document as a document object. You can action it whether it is activae, or not. The same for the docCurrent object.
So the code above gets the reference doc, sets it as an object.
The getting each paragraph as a string is a Function, with the reference document passed in as a parameter, and a counter - the Paragraph count of the reference document.[vba]
For j = 1 To docRef.Paragraphs.Count
[/vba]NOTE: Important!!!! This assumes the reference document is a list of paragraphs with no "empty" paragraphs.
So, for each paragraph in the reference document, the Find gets the next word from the reference doc, strips the paragraph mark, and returns the clean word to the current iteration of the Find.
If Asc(r.text) - the current .Found - is not 32, replace the .Found with the current word from the reference doc.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules