Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-30-2010, 09:47 AM
Roscoe Roscoe is offline Replace text in multiple documents? Windows XP Replace text in multiple documents? Office 2007
Novice
Replace text in multiple documents?
 
Join Date: Mar 2010
Posts: 25
Roscoe is on a distinguished road
Default Replace text in multiple documents?

I have multiple RTF documents (signatures) with an address line that needs to be updated. Is there a way to search and replace over multiple documents?
Reply With Quote
  #2  
Old 01-27-2011, 08:45 AM
atrivedi atrivedi is offline Replace text in multiple documents? Windows 7 Replace text in multiple documents? Office 2010 (Version 14.0)
Advanced Beginner
 
Join Date: Sep 2010
Posts: 37
atrivedi is on a distinguished road
Default Replace text in multiple documents

Hello.

Please follow the directions below to replace text in multiple documents. This is the only solution to replacing text in multiple documents for Microsoft Word. There are several options if you are using another program such as Notepad, etc...

1. Move your mouse cursor to the beginning of the text you wish to copy. Hold the left mouse button and drag the cursor over the text. The text you wish to copy should now be highlighted.

2. Click the right mouse button on the text you wish to copy and select the "Copy" option from the drop-down menu.

3. Select the "-" button in the top right corner of your document to minimize the document in your program. Open the document with the text that you wish to replace by selecting the "File" option in the menu bar and choosing the "Open" option.

4. Move your mouse cursor to the beginning of the text you wish to replace. Hold the left mouse button and drag the cursor over the text. The text you wish to replace should now be highlighted.

5. Click the right mouse button on the text you wish to replace and select the "Paste" option from the drop-down menu. The text you have copied will replace the text you have selected.

6. Repeat steps 3 through 5 if you wish to copy the text to another document.
Reply With Quote
  #3  
Old 01-27-2011, 08:44 PM
Roscoe Roscoe is offline Replace text in multiple documents? Windows XP Replace text in multiple documents? Office 2007
Novice
Replace text in multiple documents?
 
Join Date: Mar 2010
Posts: 25
Roscoe is on a distinguished road
Default

Funny. I trying to make a change in over a hundred documents. I was hoping for a "replace all" that could do that.
Reply With Quote
  #4  
Old 01-28-2011, 02:09 AM
macropod's Avatar
macropod macropod is offline Replace text in multiple documents? Windows 7 32bit Replace text in multiple documents? Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,950
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by atrivedi View Post
Please follow the directions below to replace text in multiple documents. This is the only solution to replacing text in multiple documents for Microsoft Word.
All I can say to that, is nonsense! A macro can be used to automate searching across as many files as you want.

Roscoe: Try a macro like:
Code:
Sub Update_RTF_File_Sigs()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, RTFDoc
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.rtf", vbNormal)
While strFile <> ""
  Set RTFDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  With RTFDoc.Range.Find
    .ClearFormatting
    .Text = "Old Address"
    .Replacement.Text = "New Address"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
  End With
  RTFDoc.Close SaveChanges:=True
  strFile = Dir()
Wend
Set RTFDoc = Nothing
Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
The macro allows you to browse to the folder containing the rtf files you want to process, then processes all such files in that folder automatically.

Of course, you'll need to change the 'Old Address' & 'New Address' details to suit your requirements. In this regard, if an address is in the form of:
Address Line 1
Address Line 2
the .Text and/or .Replacement.Text expressions (as required), would become:
= "Address Line 1^pAddress Line2"

In case you need advide on how to install & run a macro, see: http://www.gmayor.com/installing_macro.htm
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]

Last edited by macropod; 01-28-2011 at 02:17 AM. Reason: Deleted unnecessary message box (left over from testing)
Reply With Quote
  #5  
Old 03-23-2016, 01:48 AM
razeryokes razeryokes is offline Replace text in multiple documents? Windows 7 64bit Replace text in multiple documents? Office 2010 64bit
Novice
 
Join Date: Mar 2016
Posts: 1
razeryokes is on a distinguished road
Default same script including sub-directories ?

Hi,

Thank you so much macropod ! This script is exactly what I am looking for. Would it be possible to also check in all sub-directories of the selected folders ? That would be amazing.

Thank you !

~Pat
Reply With Quote
  #6  
Old 03-23-2016, 02:31 AM
macropod's Avatar
macropod macropod is offline Replace text in multiple documents? Windows 7 64bit Replace text in multiple documents? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,950
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

For the kind of code modifications needed to process sub-folders, see: https://www.msofficeforums.com/word-...html#post47785
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 07-31-2017, 07:35 AM
kris126 kris126 is offline Replace text in multiple documents? Windows 10 Replace text in multiple documents? Office 2013
Novice
 
Join Date: Jul 2017
Posts: 1
kris126 is on a distinguished road
Default

Unfortunately none of the above solutions finds/replaces text in headers/footers in word-documents. It only affects bodytext.

Any solutions to include headers & footers in find/replace function?
Reply With Quote
  #8  
Old 07-31-2017, 04:02 PM
macropod's Avatar
macropod macropod is offline Replace text in multiple documents? Windows 7 64bit Replace text in multiple documents? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,950
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

There are numerous threads in this forum in which the code to process headers, footers, etc. is discussed - and provided.

Code that finds & replaces a specified string anywhere in all documents in a selected folder and its sub-folders might look like:
Code:
Option Explicit
Dim FSO As Object, oFolder As Object, StrFolds As String
 
Sub Main()
Application.ScreenUpdating = False
Dim TopLevelFolder As String, TheFolders As Variant, aFolder As Variant, i As Long
TopLevelFolder = GetFolder
StrFolds = vbCr & TopLevelFolder
If FSO Is Nothing Then
  Set FSO = CreateObject("Scripting.FileSystemObject")
End If
'Get the sub-folder structure
Set TheFolders = FSO.GetFolder(TopLevelFolder).SubFolders
For Each aFolder In TheFolders
  RecurseWriteFolderName (aFolder)
Next
'Process the documents in each folder
For i = 1 To UBound(Split(StrFolds, vbCr))
  Call UpdateDocuments(CStr(Split(StrFolds, vbCr)(i)))
Next
Application.ScreenUpdating = True
End Sub

Sub RecurseWriteFolderName(aFolder)
Dim SubFolders As Variant, SubFolder As Variant
Set SubFolders = FSO.GetFolder(aFolder).SubFolders
StrFolds = StrFolds & vbCr & CStr(aFolder)
On Error Resume Next
For Each SubFolder In SubFolders
  RecurseWriteFolderName (SubFolder)
Next
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

Sub UpdateDocuments(oFolder As String)
Dim strFldr As String, strFile As String, wdDoc As Document
Dim Rng As Range, Sctn As Section, HdFt As HeaderFooter, Shp As Shape
strFldr = oFolder
If strFldr = "" Then Exit Sub
strFile = Dir(strFldr & "\*.doc", vbNormal)
While strFile <> ""
  Set wdDoc = Documents.Open(FileName:=strFldr & "\" & strFile, AddToRecentFiles:=False, ReadOnly:=False, Visible:=False)
  With wdDoc
    'Loop through all story ranges
    For Each Rng In .StoryRanges
      Call FndRepRng(Rng)
      For Each Shp In Rng.ShapeRange
        If Not Shp.TextFrame Is Nothing Then
          Call FndRepRng(Shp.TextFrame.TextRange)
        End If
      Next
    Next
    'Loop through all headers & footers
    For Each Sctn In .Sections
      For Each HdFt In Sctn.Headers
        With HdFt
          If .Exists = True Then
            If .LinkToPrevious = False Then
              Call FndRepRng(HdFt.Range)
              For Each Shp In .Shapes
                If Not Shp.TextFrame Is Nothing Then
                  Call FndRepRng(Shp.TextFrame.TextRange)
                End If
              Next
            End If
          End If
        End With
      Next
      For Each HdFt In Sctn.Footers
        With HdFt
          If .Exists = True Then
            If .LinkToPrevious = False Then
              Call FndRepRng(HdFt.Range)
              For Each Shp In .Shapes
                If Not Shp.TextFrame Is Nothing Then
                  Call FndRepRng(Shp.TextFrame.TextRange)
                End If
              Next
            End If
          End If
        End With
      Next
    Next
    'Save and close the document
    .Close SaveChanges:=wdSaveChanges
  End With
  strFile = Dir()
Wend
Set wdDoc = Nothing
End Sub

Sub FndRepRng(Rng As Range)
With Rng.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "Find string"
  .Replacement.Text = "Replace string"
  .Forward = True
  .Wrap = wdFindContinue
  .Format = True
  .Execute Replace:=wdReplaceAll
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace All with plain text containing subscript DeaducK Word 0 06-24-2010 08:16 PM
Replace formatting with text eyehefbee Word 2 11-09-2009 02:41 AM
Replace text in multiple documents? Generating multiple documents from 1 data source themangoagent Word 2 08-14-2009 12:12 PM
2007 merging multiple documents into one master hugheso Word 0 04-02-2009 04:31 AM
Replace text in multiple documents? page numbering across multiple documents reitdesign Word 3 12-12-2008 11:55 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:13 PM.


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