Skip to main content

How to search and replace across multiple files in word?

If you have several dozen word files which contain the same content (such as Header, footer, some special words or number), and you need to replace the same content across those documents in Word. How would it be easier for you to get it done quickly? Certainly, you can open those files one by one to replace the same content, but it will be time-consuming and troublesome. This tutorial will show you a tricky way to replace same content within multiple documents in Word at once.

Find and replace texts across multiple word documents at the same time with VBA code
Easily find and replace different texts across multiple documents at the same time with Kutools for Word


Find and replace texts across multiple word documents at the same time with VBA code

1. Press Alt + F11 to open the Microsoft Visual Basic for Applications window.

2. In the Microsoft Visual Basic for Applications window, click InsertModule, then copy  the following VBA code into the Module window.

VBA Code: Searching and replacing same content across multiple documents at one time

Sub CommandButton1_Click()
'Updated by Extendoffice 20180625
Dim xFileDialog As FileDialog, GetStr(1 To 100) As String '100 files is the maximum applying this code
Dim xFindStr As String
Dim xReplaceStr As String
Dim xDoc As Document
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFilePicker)
With xFileDialog
    .Filters.Clear
    .Filters.Add "All WORD File ", "*.docx", 1
    .AllowMultiSelect = True
    i = 1
    If .Show = -1 Then
        For Each stiSelectedItem In .SelectedItems
            GetStr(i) = stiSelectedItem
            i = i + 1
        Next
        i = i - 1
    End If
    Application.ScreenUpdating = False
    xFindStr = InputBox("Find what:", "Kutools for Word", xFindStr)
    xReplaceStr = InputBox("Replace with:", "Kutools for Word", xReplaceStr)
    For j = 1 To i Step 1
        Set xDoc = Documents.Open(FileName:=GetStr(j), Visible:=True)
        Windows(GetStr(j)).Activate
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = xFindStr  'Find What
            .Replacement.Text = xReplaceStr  'Replace With
            .Forward = True
            .Wrap = wdFindAsk
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchByte = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
        Application.Run macroname:="NEWMACROS"
        ActiveDocument.Save
        ActiveWindow.Close
    Next
    Application.ScreenUpdating = True
End With
MsgBox "Operation end, please view", vbInformation
End Sub

3. Press the F5 key to run the code.

4. In the opening Browse window, please find and select the documents you will find and replace text inside, and then click the OK button. See screenshot:

5. In the first Kutools for Word dialog box, enter the text you will find across documents into the Find what box, and then click the OK button.

6. In the second Kutools for Word dialog box, enter the text you will replace with, and click the OK button.

8. Click the OK button in the next Microsoft Word dialog box to finish the find and replace.

In this case, all words "Word" in selected documents are replaced with "Excel" at the same time.


Easily find and replace different texts across multiple documents at the same time with Kutools for Word

Here highly recommend the Batch Find and Replace feature of Kutools for Word. With this feature, you can easily find and replace different texts across multiple Word documents as the same time. Let's see how to apply this feature to find and replace texts across documents.

Kutools for Word : with more than 100 handy Word add-ins, free to try with no limitation in 60 days.

1. Please click Kutools Plus > Batch Find and Replace to enable the feature.

2. In the Batch Find and Replace dialog box, please configure as follows.

  • 2.1) Click the Add Row button under the Find and Replace tab;
  • 2.2) In the created row fields:
A. Enter the text you will find into the textbox in the Find column
B. Enter the text you will replace with into the textbox in the Replace column;
C. Specify a search type, where to find the text and a color to highlight the text as you need. In this case, I select Full word matching, Main document and a certain color from the Search Type, Find in and Highlight columns;
  • 2.3) Repeat the step 2.1 and 2.2 to add more texts you will find and replace;
  • 2.4) Click the  button in the File Type section to add the Word documents in which you will find and replace texts;
  • 2.5) Click the Replace or Find button. See screenshot:

Notes:
1. If you click the Find button, all finding results will be displayed under the Preview Result tab. After previewing the results, if you want to replace all texts, please click the Find and Replace tab. Otherwise, close the dialog.

2. If you click the Replace button, all specified texts will be replaced with new ones immediately, and the results will also be displayed under the Preview Result tab.

3. If you specify highlight colors in step 2, all replaced texts will be highlighted with specified colors, and you can easily find them at a glance in documents.

3. Close the Batch Find and Replace dialog box

For more information about this feature, please click here....

If you want to have a free trial of this utility, please go to free download the software first, and then go to apply the operation according above steps.


Recommended Word Productivity Tools

 

Kutools For Word - More Than 100 Advanced Features For Word, Save Your 50% Time

  • Complicated and repeated operations can be done one-time processing in seconds.
  • Insert multiple images across folders into Word document at once.
  • Merge and combine multiple Word files across folders into one with your desired order.
  • Split the current document into separate documents according to heading, section break or other criteria.
  • Convert files between Doc and Docx, Docx and PDF, collection of tools for common conversions and selection, and so on...
Comments (70)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
thanks for -How To Search And Replace Across Multiple Files In Word?

what about from out side (by folder) ?

thanks gain
This comment was minimized by the moderator on the site
This is a great explanation.  Is there a way to change settings, e.g. page size, simultaneously? 
This comment was minimized by the moderator on the site
After pressing F5, I get the following error" "Compile error: User-defined type not defined." Can someone help?
This comment was minimized by the moderator on the site
Hi Monique,Please check if the Microsoft Office Object Library is enabled as shown in the screenshot below.
This comment was minimized by the moderator on the site
This is SUCH a time saving solution. THANK YOU!
One thing that may make it even better, is adding support for subdirectories. (That is: Files in multiple subdirectories)
I am an absolute newbie in this, and don't know the syntax, the parameter, or where to add that.

Could somebody help?
This comment was minimized by the moderator on the site
I need to replace text in multiple Word files and have the replaced text highlighted. Is there a way to do this?
This comment was minimized by the moderator on the site
Hi Daphne,
Why not try the Batch Find and Replace feature of Kutools for Word? It can solve this issue with ease.
This comment was minimized by the moderator on the site
this was a lifesaver thank you so much
This comment was minimized by the moderator on the site
Is it possible to replace in the headers of the word doc?
This comment was minimized by the moderator on the site
Hi Tiago,
Please apply the below VBA code to include the content of headers and footers when finding and replacing texts in multiple documents at once.

Sub CommandButton1_Click()
'Updated by Extendoffice 20180625
Dim xFileDialog As FileDialog, GetStr(1 To 100) As String '100 files is the maximum applying this code
Dim xFindStr As String
Dim xReplaceStr As String
Dim xDoc As Document
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFilePicker)
With xFileDialog
.Filters.Clear
.Filters.Add "All WORD File ", "*.docx", 1
.AllowMultiSelect = True
i = 1
If .Show = -1 Then
For Each stiSelectedItem In .SelectedItems
GetStr(i) = stiSelectedItem
i = i + 1
Next
i = i - 1
End If
Application.ScreenUpdating = False
xFindStr = InputBox("Find what:", "Kutools for Word", xFindStr)
xReplaceStr = InputBox("Replace with:", "Kutools for Word", xReplaceStr)
For j = 1 To i Step 1
Set xDoc = Documents.Open(FileName:=GetStr(j), Visible:=True)
Windows(GetStr(j)).Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = xFindStr 'Find What
.Replacement.Text = xReplaceStr 'Replace With
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Application.Run macroname:="NEWMACROS"
If MsgBox("Do you want to replace texts in headers and footers too? ", vbYesNo, "Kutools for Word") = vbYes Then
ActiveWindow.View.SplitSpecial = wdPanePrimaryFooter
Selection.Find.Execute Replace:=wdReplaceAll
Application.Run macroname:="NEWMACROS"
ActiveWindow.View.SplitSpecial = wdPanePrimaryHeader
Selection.Find.Execute Replace:=wdReplaceAll
Application.Run macroname:="NEWMACROS"
End If
ActiveDocument.Save
ActiveWindow.Close
Next
Application.ScreenUpdating = True
End With
MsgBox "Operation end, please view", vbInformation
End Sub
This comment was minimized by the moderator on the site
Is that possible to replace in the header of the word doc?
This comment was minimized by the moderator on the site
Thanks for sharing this information with us.
Word search and replace tool
This comment was minimized by the moderator on the site
Works perfectly but is there a similar macro to change text in the header! because this does not work with this version!
thanks
This comment was minimized by the moderator on the site
Hi,
Please apply the below VBA code to include the content of headers and footers when finding and replacing texts in multiple documents at once.

Sub CommandButton1_Click()
'Updated by Extendoffice 20180625
Dim xFileDialog As FileDialog, GetStr(1 To 100) As String '100 files is the maximum applying this code
Dim xFindStr As String
Dim xReplaceStr As String
Dim xDoc As Document
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFilePicker)
With xFileDialog
.Filters.Clear
.Filters.Add "All WORD File ", "*.docx", 1
.AllowMultiSelect = True
i = 1
If .Show = -1 Then
For Each stiSelectedItem In .SelectedItems
GetStr(i) = stiSelectedItem
i = i + 1
Next
i = i - 1
End If
Application.ScreenUpdating = False
xFindStr = InputBox("Find what:", "Kutools for Word", xFindStr)
xReplaceStr = InputBox("Replace with:", "Kutools for Word", xReplaceStr)
For j = 1 To i Step 1
Set xDoc = Documents.Open(FileName:=GetStr(j), Visible:=True)
Windows(GetStr(j)).Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = xFindStr 'Find What
.Replacement.Text = xReplaceStr 'Replace With
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Application.Run macroname:="NEWMACROS"
If MsgBox("Do you want to replace texts in headers and footers too? ", vbYesNo, "Kutools for Word") = vbYes Then
ActiveWindow.View.SplitSpecial = wdPanePrimaryFooter
Selection.Find.Execute Replace:=wdReplaceAll
Application.Run macroname:="NEWMACROS"
ActiveWindow.View.SplitSpecial = wdPanePrimaryHeader
Selection.Find.Execute Replace:=wdReplaceAll
Application.Run macroname:="NEWMACROS"
End If
ActiveDocument.Save
ActiveWindow.Close
Next
Application.ScreenUpdating = True
End With
MsgBox "Operation end, please view", vbInformation
End Sub
This comment was minimized by the moderator on the site
Oh yeah! Thank you sooooo much! This is a life-saver!
This comment was minimized by the moderator on the site
Doesn't work for me on footers. e.g find FFSOSxxxx and replace with GGSOSxxxx, i can see this code is great for others pls help. :(
This comment was minimized by the moderator on the site
Hi there, is there a code for replacing with text that is multiple sentences, with bullet points and quotation marks in MS word using this code - which is fantastic !
This comment was minimized by the moderator on the site
hi, i want to replace a number but i guess the problem that it's not working is that the number is not written in english it's in Persian. I would be so thankfull if someone can help me with it.
This comment was minimized by the moderator on the site
sorry, i was wrong. the problem is that the number is in the header and it seems that the trick is not working in header. if i open one of the files and activate the header then run the module it works but just in that specific file ,even if i select all the files. thanks again.
This comment was minimized by the moderator on the site
Hi, I new to VBA. I am able to get the data updated for only single file. How to select multiple files when the VBA script opens up ? The question might be simple to few, but it will help. Since i need to update a string in 175+ plus word files. Appreciate your quick response. Thanks ...
This comment was minimized by the moderator on the site
Put your files in one folder and select them all with a shift key. Hope this works.
This comment was minimized by the moderator on the site
Hi
this code is only editing 7 documents at a time... why?
This comment was minimized by the moderator on the site
I really appreciated this post. Thank you. It allowed me to spend 10 minutes implementing something that saved several hours of work.
This comment was minimized by the moderator on the site
What if I want to find and replace multiple sentences in multiple documents?
This comment was minimized by the moderator on the site
Thank you so much. You win the internet!

This has saved me amending 360 documents.

You're my favourite :)
This comment was minimized by the moderator on the site
worked like a charm for me. Thanks a billion!
This comment was minimized by the moderator on the site
Merlin 4099, I am having trouble with the following portion of your code. I am new to VBA.: ' --------------- --------------- --------------- ---------- ' Beginning Header Updates ' --------------- --------------- --------------- ---------- Windows(GetStr(j)).Activate Selection.Find.ClearFormatting Selection.Find.Replacement.Cle arFormatting If ActiveWindow.View.SplitSpecialwdPane None Then ActiveWindow.Panes(2).Close End If If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _ ActivePane.View .Type = wdOutlineView Then Can anyone help me? I think I fixed most
This comment was minimized by the moderator on the site
Was figuring out this issue myself, today.

First, make sure you fix the line:

Selection.Find.Replacement.Cle arFormatting

To be:

Selection.Find.Replacement.ClearFormatting

Then, change the following line:

If ActiveWindow.View.SplitSpecialwdPane None Then

To look like this:

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then


The code should look like this in the end:

Windows(GetStr(j)).Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If

If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
This comment was minimized by the moderator on the site
Merlin4099, I am having trouble with the following portion of your code: ' --------------- --------------- --------------- ---------- ' Beginning Header Updates ' --------------- --------------- --------------- ---------- Windows(GetStr(j)).Activate Selection.Find.ClearFormatting Selection.Find.Replacement.Cle arFormatting If ActiveWindow.View.SplitSpecialwdPane None Then ActiveWindow.Panes(2).Close End If If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _ ActivePane.View .Type = wdOutlineView Then Can you or anyone else help me? Thanks!
This comment was minimized by the moderator on the site
Thanks so much for posting this, absolute lifesaver! I notice that there is a line "MatchWildcards = False", which implies that it's possible to use wildcards in the search text - is that possible and, if so, what's the protocol? Thanks again!
This comment was minimized by the moderator on the site
Hi all, I am new to the whole VBA thing and I Love love love this code! I have 5000+ docs to review and if this works it will save heaps of time! I have the code above running however i can see that it does not cater for replacing text in footers, Only headers and the body of the doc. Can anyone assist in extending this to the entire document. P.S. not sure if this is relevant but the list of words that i need to replace is the same for headers, footers and the body... Any help would really be appreciated!
This comment was minimized by the moderator on the site
You can edit the footer by using almost identical code that the header uses. Copy everything from "Begin Header Updates" to "End Header Updates" and paste it directly after.

Then, in this pasted section, find the word "wdSeekCurrentPageHeader" and change it to "wdSeekCurrentPageFooter".
This comment was minimized by the moderator on the site
Out of interest what does the line: Application.Run macroname:="NEW MACROS" do? The rest of the code makes sense to me (used to VBA in Excel) but I can't figure out why you need to execute another piece of code here. Thanks for the great tool though.
This comment was minimized by the moderator on the site
This macro was amazing..is there any code to update multiple excel files,spss files.also is there is any macro to update the header and footer on multiple word documents . Any help is appreciated .thanks !
This comment was minimized by the moderator on the site
Just brilliant, saved me lots of time, thank you
This comment was minimized by the moderator on the site
You can always try a 3rd party tool to do this which makes it very easy: http://www.officefindreplace.com
This comment was minimized by the moderator on the site
I am trying to add an entire section to a document that has three lines, bolding, and bullet points. Is there a way to keep that formatting when running the macro?
This comment was minimized by the moderator on the site
To Merlin4099 You have just saved my life (... time...) with you code ! THANK YOU !!!
This comment was minimized by the moderator on the site
Is it possible to change code and search and replace photo in multiply files?
This comment was minimized by the moderator on the site
this was sooo helpful, thank you very much! I have just changed the contents in 90 word files. This is amazing! Just one more tip: at the beginning close all open word documents but one of your file to be change.
This comment was minimized by the moderator on the site
You can use a program such as [url]http://www.officefindreplace.com[/url] to perform the same operation on multiple documents, or multiple operations on the same document. [url]http://www.officefindreplace.com[/url]
This comment was minimized by the moderator on the site
' --- Beging body text replacemnt 3 --- With Selection.Find .Text = "leadership" ' Find What .Replacement.Text = "supervision" ' Replace With .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchByte = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll ' --- End body text replacemnt 3 --- Application.Run macroname:="NEWMACROS" ActiveDocument.Save ActiveWindow.Close ' ------------------------------------------------------- ' End of Body Updates ' ------------------------------------------------------- Next Application.ScreenUpdating = True End With MsgBox "operation end, please view", vbInformation End Sub
This comment was minimized by the moderator on the site
To Merlin4099 Wonderful code - thank you!! worked perfectly in executing what I needed.
This comment was minimized by the moderator on the site
' --- Beging body text replacemnt 1 --- With Selection.Find .Text = "Body text 1" ' Find What .Replacement.Text = "BT1" ' Replace With .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchByte = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll ' --- Beging body text replacemnt 1 --- ' --- Beging body text replacemnt 2 --- With Selection.Find .Text = "membership" ' Find What .Replacement.Text = "team members" ' Replace With .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchByte = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll ' --- End body text replacemnt 2 ---
This comment was minimized by the moderator on the site
' --- Beging header text replacemnt 2 --- With Selection.Find .Text = "Header String 2" ' Find What .Replacement.Text = "HReplacement 2" ' Replace With .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchByte = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False If .Found = True Then ChangeMade = True End If End With Selection.Find.Execute Replace:=wdReplaceAll ' --- Beging header text replacemnt 2 --- ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument ' ------------------------------------------------------ ' End of Header Updates ' ------------------------------------------------------ ' ------------------------------------------------------- ' Beginning of Body Updates ' ------------------------------------------------------- Windows(GetStr(j)).Activate Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ' Delete additional body replacement blocks if ' not needed or copy/paste additional blocks if ‘ required. ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This comment was minimized by the moderator on the site
' --- Beging header text replacemnt 1 --- With Selection.Find .Text = "Header String 1" ' Find What .Replacement.Text = "HReplacement 1" ' Replace With .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchByte = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False If .Found = True Then ChangeMade = True End If End With Selection.Find.Execute Replace:=wdReplaceAll ' --- End header text replacemnt 1 ---
This comment was minimized by the moderator on the site
Sub Search_and_Replace() ' 500 files is the maximum applying this code Dim MyDialog As FileDialog, GetStr(1 To 500) As String On Error Resume Next Set MyDialog = Application.FileDialog(msoFileDialogFilePicker) ' --------------------------------------------------------------- ' *.doc? allows processing of *.doc and *.docx files. ' --------------------------------------------------------------- With MyDialog .Filters.Clear .Filters.Add "All WORD File ", "*.doc?", 1 .AllowMultiSelect = True i = 1 If .Show = -1 Then For Each stiSelectedItem In .SelectedItems GetStr(i) = stiSelectedItem i = i + 1 Next i = i - 1 End If Application.ScreenUpdating = False For j = 1 To i Step 1 Set Doc = Documents.Open(FileName:=GetStr(j), Visible:=True) ' ------------------------------------------------------- ' Beginning Header Updates ' ------------------------------------------------------- Windows(GetStr(j)).Activate Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting If ActiveWindow.View.SplitSpecial wdPaneNone Then ActiveWindow.Panes(2).Close End If If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _ ActivePane.View.Type = wdOutlineView Then ActiveWindow.ActivePane.View.Type = wdPrintView End If ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting
This comment was minimized by the moderator on the site
I ran across this code while searching with DuckDuckGo. I modified it to suit my needs which were to other requests such as handling multiple search/replace in the header and multiple search/replace in the body. I also modified it handle the older .DOC format and the new .DOCX format. The modified code is pasted below. Feel free to use and improve! It is being sent in multiple parts because comments to this site can only be 2000 characters or less. ------------------------------------------------------------
This comment was minimized by the moderator on the site
This code has saved so much time for me... so, thanks! One thing though - is there any way to increase the number of words that the macro finds/replaces? I need to find/replace multiple paragraphs at once and the macro seems to have a limit of approximately 200 characters.
This comment was minimized by the moderator on the site
Is it possible to tweak this macro to perform multiple 'find and replace' stances at once? There are several words and phareses I need to replace and as of now I need to reinsert the VBA code for every instance. I'm a noob regarding those things
This comment was minimized by the moderator on the site
I've been testing it for the last few minutes and it's really a wonder, thank you! I need to perform several 'find and replace' routines for several files. Dozens of routines for dozens of files. Is there a way to 'save' the module so I don't need to paste it again on the VBA screen every time I need to perform a new routine?
This comment was minimized by the moderator on the site
I am sorry but I have no idea re the headers or the format. It was not something I needed to include in my code. I had never used this before myself and the code I posted was taken from the original code above and then tweeked using code found in other searches until it worked. I am sorry I cant be of more help.
This comment was minimized by the moderator on the site
Thanks for posting this! I've run a couple tests and gotten it to work for me; however, it's not working on text in headers and footers for some reason. Is there a fix for this?
This comment was minimized by the moderator on the site
Big THX 4 the code... but also have that prblm with the headers... no change Thanks in advance
This comment was minimized by the moderator on the site
I want to change the format of the replaces text. i.e. "Pizza" has to be replaced with "Burger". How can it be achieved?
This comment was minimized by the moderator on the site
Hello, This worked great for the word in the body of the document but did not replace the same word in the header. Any ideas. Thanks
This comment was minimized by the moderator on the site
This worked perfectly and saved me so much time. Thank you!
This comment was minimized by the moderator on the site
It only runs across selected files and files must be in one folder only. This is run as a macro in a word doc. It is run from clicking a button. It only updates word doc's Hope this helps. Private Sub CommandButton1_Click() Dim MyDialog As FileDialog, GetStr(1 To 100) As String '100 files is the maximum applying this code On Error Resume Next Set MyDialog = Application.FileDialog(msoFileDialogFilePicker) With MyDialog .Filters.Clear '~~> Change this file extention to the relevant doc type. .Filters.Add "All WORD File ", "*.doc", 1 .AllowMultiSelect = True i = 1 If .Show = -1 Then For Each stiSelectedItem In .SelectedItems GetStr(i) = stiSelectedItem i = i + 1 Next i = i - 1 End If Application.ScreenUpdating = False For j = 1 To i Step 1 Set doc = Documents.Open(FileName:=GetStr(j), Visible:=True) Windows(GetStr(j)).Activate Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting For i = 1 To doc.Hyperlinks.Count 'If the hyperlink matches. If LCase(doc.Hyperlinks(i).Address) = "http://www.yahoo.co.uk/" Then 'Change the links address. doc.Hyperlinks(i).Address = "https://www.google.co.uk" 'Change the links display text if desired. doc.Hyperlinks(i).TextToDisplay = "http://www.google.co.uk" End If Next Application.Run macroname:="NEWMACROS" ActiveDocument.Save ActiveDocument.Close Next Application.ScreenUpdating = True End With MsgBox "operation end, please view", vbInformation End Sub
This comment was minimized by the moderator on the site
You then add one of the following two sections depending on if it is test or hyperlink to be replaced '~~> This section if uncommented will replace text - ONLY ONE OF TWO SECTIONS CAN BE UNCOMMENTED AT ONCE With Selection.Find .Text = "http://www.google.co.uk" 'Find What .Replacement.Text = "http://www.google.com" 'Replace With .Forward = True .Wrap = wdFindAsk .Format = False .MatchCase = False .MatchWholeWord = False .MatchByte = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll Application.Run macroname:="NEWMACROS" ActiveDocument.Save ActiveDocument.Close Next Application.ScreenUpdating = True End With MsgBox "operation end, please view", vbInformation '~~> This section if uncommented will replace hyperlinks 'For i = 1 To doc.Hyperlinks.Count ' 'If the hyperlink matches. ' If LCase(doc.Hyperlinks(i).Address) = "http://www.google.co.uk/" Then ' 'Change the links address. ' doc.Hyperlinks(i).Address = "https://www.google.com" ' 'Change the links display text if desired. ' doc.Hyperlinks(i).TextToDisplay = "http://www.google.com" ' End If ' Next 'Application.Run macroname:="NEWMACROS" 'ActiveDocument.Save 'ActiveDocument.Close 'Next 'Application.ScreenUpdating = True 'End With 'MsgBox "operation end, please view", vbInformation End Sub
This comment was minimized by the moderator on the site
Hi Pia, I am trying to run your script for replacing hyperlinks but am getting errors. Any chance you can post the script for replacing hyperlinks all in 1 piece? I must be messing something up when trying to patch your code together.
This comment was minimized by the moderator on the site
This is what finally worked for me. I hope it helps. I added this macro to a word file. It will run the replace any documents selected (you can select as many as you like) when the file dialoge is open. It wont work on sub folders so I did each folder in the file structure individually but it worked.It may not be the best fix but it works at least. Private Sub CommandButton1_Click() Dim MyDialog As FileDialog, GetStr(1 To 100) As String '100 files is the maximum applying this code On Error Resume Next Set MyDialog = Application.FileDialog(msoFileDialogFilePicker) With MyDialog .Filters.Clear '~~> Change this file extention to the relevant doc type. .Filters.Add "All WORD File ", "*.doc", 1 .AllowMultiSelect = True i = 1 If .Show = -1 Then For Each stiSelectedItem In .SelectedItems GetStr(i) = stiSelectedItem i = i + 1 Next i = i - 1 End If Application.ScreenUpdating = False For j = 1 To i Step 1 Set doc = Documents.Open(FileName:=GetStr(j), Visible:=True) Windows(GetStr(j)).Activate Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting 'Section then either one or the other of sections of code to follow.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations