VBA Word - Bold specific words based on a list
Solved
Skt44
-
Skt44 Posted messages 2 Status Membre -
Skt44 Posted messages 2 Status Membre -
Hello,
I want to create a macro that makes certain words bold in a WORD document according to a list.
I have only been able to do this for a specific word "Bold".
I would like to do this for a larger list of words that varies from text to text. How can I do this?
Thank you
""Sub Macro4()
'
' Macro2 Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = True
Selection.Find.Replacement.ClearFormatting
Dim wordList As Variant
Dim i As Integer
wordList = Array("Word1", "Word2", "Word3")
For i = LBound(wordList) To UBound(wordList)
With Selection.Find
.Text = wordList(i)
.Replacement.Text = wordList(i)
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next i
End Sub ""
I want to create a macro that makes certain words bold in a WORD document according to a list.
I have only been able to do this for a specific word "Bold".
I would like to do this for a larger list of words that varies from text to text. How can I do this?
Thank you
""Sub Macro4()
'
' Macro2 Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = True
Selection.Find.Replacement.ClearFormatting
Dim wordList As Variant
Dim i As Integer
wordList = Array("Word1", "Word2", "Word3")
For i = LBound(wordList) To UBound(wordList)
With Selection.Find
.Text = wordList(i)
.Replacement.Text = wordList(i)
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next i
End Sub ""
13 réponses
yg_be
Posted messages
23437
Registration date
Status
Contributeur
Last intervention
Ambassadeur
1 588
Hello,
When you say "which varies from one text to another," do you mean "which varies from one document to another"?
So you have a list of document names, and for each document, a list of words?
Do you intend to have a macro for each document?
When you say "which varies from one text to another," do you mean "which varies from one document to another"?
So you have a list of document names, and for each document, a list of words?
Do you intend to have a macro for each document?
Hello,
Yes, these are texts of 2000 words or more and the list varies for each document. I was imagining a macro that adapts, but I'm not sure if that's possible.
Yes, these are texts of 2000 words or more and the list varies for each document. I was imagining a macro that adapts, but I'm not sure if that's possible.
When you write "text", is it equivalent to document? It's clearer not to change vocabulary.
This will only be possible if you describe what you want.
How do you plan to use this macro?
Would it be in a single document and would it deal with other documents? Which ones, when?
How would you like this to happen?
This will only be possible if you describe what you want.
How do you plan to use this macro?
Would it be in a single document and would it deal with other documents? Which ones, when?
How would you like this to happen?
Indeed, for me, text = document in my mind.
I cannot answer you regarding the usage, it will depend on what is possible to do. I can adapt.
Actually, I want to save time and automate a repetitive task. Currently, I have to make a list of about a dozen words bold in every document. It's quite time-consuming.
I cannot answer you regarding the usage, it will depend on what is possible to do. I can adapt.
Actually, I want to save time and automate a repetitive task. Currently, I have to make a list of about a dozen words bold in every document. It's quite time-consuming.
I imagined:
1- I open my document (with my text)
2- I add the list of words to my document (or in an Excel file next to it)
3- I activate the Macro from my Word
4- The words from my list become bold
1- I open my document (with my text)
2- I add the list of words to my document (or in an Excel file next to it)
3- I activate the Macro from my Word
4- The words from my list become bold
Is my document and my Word the same thing?
Is the macro already present in the document?
Do you always work with the same document?
Do you use the list of words multiple times?
Where is this list prepared?
If you have multiple lists, how do you choose which one?
An example:
Is the macro already present in the document?
Do you always work with the same document?
Do you use the list of words multiple times?
Where is this list prepared?
If you have multiple lists, how do you choose which one?
An example:
sub macro4() Dim mots() As Variant, mot mots = Array("Bold", "unautremot", "encoreunautremot") For Each mot In mots call engraisse(mot) Next mot end sub private Sub engraisse(unmot as string) Selection.Find.ClearFormatting Selection.Find.Font.Bold = True Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = unmot .Replacement.Text = unmot .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll"" End Sub
- my document and my Word, is it the same thing? No
- is the macro already present in the document? No
- do you always work with the same document? No
- do you use the word list multiple times? A different list each time
- where is this list prepared? Excel generally
- if you have multiple lists, how do you choose which one? A list corresponds to a text
- is the macro already present in the document? No
- do you always work with the same document? No
- do you use the word list multiple times? A different list each time
- where is this list prepared? Excel generally
- if you have multiple lists, how do you choose which one? A list corresponds to a text
Absolutely, one document at a time, and according to the title of the document, I associate the right list of words.
yg_be
Posted messages
23437
Registration date
Status
Contributeur
Last intervention
Ambassadeur
1 588
I suggest doing this:
- have a macro in a Word document
- when this macro is executed, it will examine all the open documents, and if the document name is in a list of documents, it will bold a list of words.
- these two lists could be extracted from an Excel document.
Before going further, I created a small prototype, which doesn't use lists yet, but where the name of the document to be modified, as well as the list of words, is in the macro code.
Can you test this and indicate if it is heading in the right direction?
- have a macro in a Word document
- when this macro is executed, it will examine all the open documents, and if the document name is in a list of documents, it will bold a list of words.
- these two lists could be extracted from an Excel document.
Before going further, I created a small prototype, which doesn't use lists yet, but where the name of the document to be modified, as well as the list of words, is in the macro code.
Can you test this and indicate if it is heading in the right direction?
Option Explicit Private Sub browsedocs() Dim wDoc As Document For Each wDoc In Application.Documents If wDoc.Name = "testupd.docx" Then Call undoc(wDoc, "Bold,unautremot,encoreunautremot") End If Next wDoc End Sub Private Sub undoc(doc As Document, lesmots As String) Dim mots As Variant, mot, smot As String mots = Split(lesmots, ",") Dim r As Range Set r = doc.Range r.WholeStory For Each mot In mots smot = mot Call engraisse(r, smot) Next mot End Sub Private Sub engraisse(r As Range, unmot As String) With r.Find .ClearFormatting .Replacement.ClearFormatting .Replacement.Font.Bold = True .Text = unmot .Replacement.Text = unmot .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute Replace:=wdReplaceAll End With End Sub Private Sub engraisseok(doc As Document, unmot As String) doc.Activate Selection.WholeStory With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Replacement.Font.Bold = True .Text = unmot .Replacement.Text = unmot .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute Replace:=wdReplaceAll End With End Sub
To retrieve names in Excel:
- create an Excel file (leslistes.xlsx) in the same directory as the document with the macro
- in the Excel file, in a sheet named "list", have a list of document names starting from A2, and next to it (B2), a list with the words to search for (in one cell, all the words separated by a comma. Therefore, one line per candidate document.
- in the VBA editor of Word, where the macro is, add "Microsoft Excel ... Object Library" in the references
- replace the sub browsedocs with this:
- create an Excel file (leslistes.xlsx) in the same directory as the document with the macro
- in the Excel file, in a sheet named "list", have a list of document names starting from A2, and next to it (B2), a list with the words to search for (in one cell, all the words separated by a comma. Therefore, one line per candidate document.
- in the VBA editor of Word, where the macro is, add "Microsoft Excel ... Object Library" in the references
- replace the sub browsedocs with this:
Sub browsedocs() Dim wDoc As Document, wb As Excel.Workbook, nomdoc As Excel.Range, ws As Excel.Worksheet Set wb = Excel.Workbooks.Open(ActiveDocument.Path + "/leslistes.xlsx") Set ws = wb.Sheets("list") For Each wDoc In Application.Documents Set nomdoc = ws.Cells(2, 1) Do While nomdoc <> "" If wDoc.Name = nomdoc Then Call undoc(wDoc, nomdoc.Offset(, 1).Value) Exit Do End If Set nomdoc = nomdoc.Offset(1) Loop Next wDoc wb.Close End Sub