Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-15-2023, 01:42 PM
jillmars jillmars is offline VBA code for highlighting across multiple Word docs? Windows 11 VBA code for highlighting across multiple Word docs? Office 2021
Novice
VBA code for highlighting across multiple Word docs?
 
Join Date: Mar 2023
Posts: 2
jillmars is on a distinguished road
Default VBA code for highlighting across multiple Word docs?

Hello! I have no idea what I'm doing when it comes to VBA codes, but I know that I need one now. I have a giant folder full of Word documents, and I need to find one specific word/phrase ("prompt," - with the word & the comma), highlight it, and do this across all documents in a selected folder. I've used the following VBA code for finding/replacing across multiple documents, but I wasn't sure how to edit it so that I can find/highlight across multiple documents. Any help would be GREATLY appreciated - thank you!


Sub CommandButton1_Click()
'Updated by Extendoffice 20180625
Dim xFileDialog As FileDialog, GetStr(1 To 500) As String '500 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
Reply With Quote
  #2  
Old 03-15-2023, 02:00 PM
gmaxey gmaxey is offline VBA code for highlighting across multiple Word docs? Windows 10 VBA code for highlighting across multiple Word docs? Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

I think you would just need to add:

.Replacement.Highlight = True


... to your With Selection.Find statement
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 03-21-2023, 04:45 AM
jillmars jillmars is offline VBA code for highlighting across multiple Word docs? Windows 11 VBA code for highlighting across multiple Word docs? Office 2021
Novice
VBA code for highlighting across multiple Word docs?
 
Join Date: Mar 2023
Posts: 2
jillmars is on a distinguished road
Default

That didn't work, unfortunately. It's still asking me to find a word and replace it with another word, but it's not highlighting anything. Would you possibly be able to copy/paste the code that you think would work? I am clueless when it comes to this stuff, so I might be doing it wrong. Thank you!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA code for highlighting across multiple Word docs? Fill multiple word docs with 1 vb userform? lordbodom Word VBA 14 08-19-2020 12:19 PM
Change dates on multiple word docs mnhim001 Word 1 08-01-2019 03:22 PM
VBA code for highlighting across multiple Word docs? Run macro to save multiple word docs and xml Prateep Nath Word VBA 1 02-05-2018 02:52 PM
Highlighting multiple word instances in text gekser Word VBA 9 07-10-2014 05:46 AM
Add watermark to multiple word docs mykeee Word 1 09-21-2013 04:01 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:28 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