#1
|
|||
|
|||
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 |
#2
|
|||
|
|||
I think you would just need to add:
.Replacement.Highlight = True ... to your With Selection.Find statement |
#3
|
|||
|
|||
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!
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |
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 |