Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-05-2021, 11:18 PM
yacov yacov is offline find replace to all open documents Windows 10 find replace to all open documents Office 2016
Competent Performer
find replace to all open documents
 
Join Date: Oct 2019
Posts: 139
yacov is on a distinguished road
Default find replace to all open documents

Hi.
I am looking for a macro to find and replace text between all open documents.


I found a lot of macros on the internet, but they all open the files and after the replacement close them. And I want the files to stay open after the replacment.

Is it possible to change such a macro so that instead of selecting a folder it will perform the operation on the files that are already open?

thanks.
Yacov
Reply With Quote
  #2  
Old 10-06-2021, 04:00 AM
Guessed's Avatar
Guessed Guessed is offline find replace to all open documents Windows 10 find replace to all open documents Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,975
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Looping through currently open documents is easy
Code:
Sub DocsOpen()
  Dim aDoc As Document
  For Each aDoc In Application.Documents
    MsgBox aDoc.Name
  Next aDoc
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 10-06-2021, 06:59 AM
yacov yacov is offline find replace to all open documents Windows 10 find replace to all open documents Office 2016
Competent Performer
find replace to all open documents
 
Join Date: Oct 2019
Posts: 139
yacov is on a distinguished road
Default

Thanks a lot.
I did not quite understand the logic, when to run the macro because sometimes it works and sometimes it does not work for me.
should I perform a search / replace in one file anf only after that need to run the macro?
Reply With Quote
  #4  
Old 10-06-2021, 09:52 AM
yacov yacov is offline find replace to all open documents Windows 10 find replace to all open documents Office 2016
Competent Performer
find replace to all open documents
 
Join Date: Oct 2019
Posts: 139
yacov is on a distinguished road
Default

I tried my luck, and I have no luck :-)
Help would be appreciated

Sub MFindReplace()
Dim aDoc As Document
Dim strFindText As String
Dim strReplaceText As String
With Selection
.HomeKey Unit:=wdStory
With Selection.Find
.Text = strFindText
.Replacement.Text = strReplaceText
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
For Each aDoc In Application.Documents
MsgBox aDoc.Name
Next aDoc
End With
End Sub
Reply With Quote
  #5  
Old 10-06-2021, 07:49 PM
Guessed's Avatar
Guessed Guessed is offline find replace to all open documents Windows 10 find replace to all open documents Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,975
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Like this
Code:
Sub MFindReplace()
  Dim aDoc As Document, strFindText As String, strReplaceText As String
  strFindText = "Finders"
  strReplaceText = "Keepers"
  For Each aDoc In Application.Documents
    With aDoc.Range.Find
      .Text = strFindText
      .Replacement.Text = strReplaceText
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      .Execute Replace:=wdReplaceAll
    End With
  Next aDoc
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 10-06-2021, 11:01 PM
yacov yacov is offline find replace to all open documents Windows 10 find replace to all open documents Office 2016
Competent Performer
find replace to all open documents
 
Join Date: Oct 2019
Posts: 139
yacov is on a distinguished road
Default

Thanks a lot Guessed. works great
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find or replace in all word documents jkathir Word 6 08-30-2019 07:27 AM
find replace to all open documents Find & Replace text in Field Code across multiple documents RPM7 Word VBA 6 05-12-2017 12:58 AM
find replace to all open documents Find & replace footer text in a folder of Word 2010 documents kennethc Word 3 03-28-2015 02:49 AM
find replace to all open documents How do I find/replace the same word in multiple documents? Ineedhelp! Word 3 03-04-2014 03:50 PM
Find and replace multiple documents change style BaPW Word 0 08-14-2011 11:12 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:50 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft