Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find replace in all open doc

Status
Not open for further replies.

nikka

Instructor
Mar 11, 2001
36
ZA
Hi, How do I get my code to apply to all open documents except the active document.

This is where I have managed to get up to.

Thanks Nikka
(MS WORD)


For Each oDocument In Application.Documents

If oDocument.FullName <> ActiveDocument.FullName Then

Selection.Find.ClearFormatting
Selection.Find.Style = oDocument.Styles(&quot;Body Text (10pt Indented)&quot;)
Selection.Find.ParagraphFormat.Borders.Shadow = False
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = oDocument.Styles( _
&quot;Body Text (10pt)&quot;)
Selection.Find.Replacement.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = &quot;&quot;
.Replacement.Text = &quot;&quot;
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End If
Next oDocument
 
Hi Nikka
I don't really have any experience of Word VBA but applying the principles used in xl your code looks ok, apart from one thing.

It would seem to me that you may have to select each document as you are using &quot;selection.find&quot;
As I say this is a bit of a guess on my part as it's word!

I've just tried this using the code below - in xl there's not much difference between select and activate when it comes to workbooks but there appears to be quite a difference in Word (maybe I'm learning something new!!)

I hope this is of some help! If my assumptions are complete 'rubbish' maybe you could let me know so I can learn a little??

Code:
Sub a()
Dim oDoc As Document
For Each oDoc In Application.Documents
    If Not oDoc = ThisDocument Then
        oDoc.Select
        MsgBox oDoc.Name
        'Find and Replace code here
        FindReplace
    End If
Next
End Sub

;-)

If a man says something and there are no women there to hear him, is he still wrong? [ponder]
The faqs ma'am, just the faqs. Get the best from these forums : faq222-2244
 
Hi Loomah,

My code now looks like this, it does the search BUT returns 0 search results it does not seem to find the styles.



Dim oDoc As Document
For Each oDoc In Application.Documents
If Not oDoc = ThisDocument Then
oDoc.Select
MsgBox oDoc.Name
'Find and Replace code here
'Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles(&quot;Body Text (10pt Indented)&quot;)
Selection.Find.ParagraphFormat.Borders.Shadow = False
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles( _
&quot;Body Text (10pt)&quot;)
Selection.Find.Replacement.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = &quot;&quot;
.Replacement.Text = &quot;&quot;
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

End If
 
Hi nikka,

The problem is, as Loomah says, that your Selection is in the ACtiveDocument. Trouble is that changing it, changes the ActiveDocument as well so your loop control fails.

You need to work with a range in oDocument. You could use the whole document but Find will use a zero-length range as a starting point to search the whole story and it is easier to type odocument.Range(0,0), so I would use that instead of Selection within your loop. Here's a copy of your code with it in, but it would be better to use a With Block.

Code:
For Each oDocument In Application.Documents
           
   If oDocument.FullName <> ActiveDocument.FullName Then
Code:
oDocument.Range(0,0)
Code:
.Find.ClearFormatting
Code:
oDocument.Range(0,0)
Code:
.Find.Style = oDocument.Styles(&quot;Body Text (10pt Indented)&quot;)
Code:
oDocument.Range(0,0)
Code:
.Find.ParagraphFormat.Borders.Shadow = False
Code:
oDocument.Range(0,0)
Code:
.Find.Replacement.ClearFormatting
Code:
oDocument.Range(0,0)
Code:
.Find.Replacement.Style = oDocument.Styles( _
        &quot;Body Text (10pt)&quot;)
Code:
oDocument.Range(0,0)
Code:
.Find.Replacement.ParagraphFormat.Borders.Shadow = False
    With
Code:
oDocument.Range(0,0)
Code:
.Find
        .Text = &quot;&quot;
        .Replacement.Text = &quot;&quot;
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
Code:
oDocument.Range(0,0)
Code:
.Find.Execute Replace:=wdReplaceAll
    End If
    Next oDocument

Enjoy,
Tony
 
THANKS The both code options work well, just a little key factor I had missing in my search.... the format.

.Execute Format:=True
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top