VBA Word - Bold specific words based on a list

Solved
Skt44 -  
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 ""

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?
0
Skt44
 
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.
0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 588
 
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?
0
Skt44
 
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.
0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 588
 
Describe how you are currently doing.
What do you do every day, every week, ...
0
Skt44
 
What does that mean?
0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 588
 
Imagine that VBA code is a person working for you.
You're going on vacation; what will you ask them to do, and when?
1
Skt44
 
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
0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 588
 
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:
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
0
Skt44
 
- 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
0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 588
 
How do you choose which document to work on?

The Excel workbook might be the right place to start the macro.
0
Skt44
 
Either I create the documents, or I receive them by email.
0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 588
 
Do you work on one document at a time?
Is it based on the name of the document that we choose the word list?
1
Skt44
 
Absolutely, one document at a time, and according to the title of the document, I associate the right list of words.
0
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?

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 
0
Skt44
 
Hello yg_be,

That's exactly right, it's working great!
0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 588
 
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:
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
0
Skt44
 
Thank you very much for these elements,

I just made the changes but I have an error "compilation error: user-defined type not defined"

It highlights "Sub browsedocs()".
0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 588
 
Have you added "Microsoft Excel ... Object Library" to the references?
There should be a text highlighted in yellow, and another highlighted in blue. What is the text in blue?
0
Skt44 Posted messages 2 Status Membre
 
Hi,

Indeed I had mistakenly unchecked the option,

However, it seems to me that I did everything correctly but nothing is happening.
0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 588
 
The name of your document is not "macro," it's more like "macro.doc" or something like that.
How did you test the prototype?
0
Skt44 Posted messages 2 Status Membre
 
Okay, it's working.

A big thank you yg_be for your help. The time and productivity gain thanks to the macro is extremely valuable to me.

Thank you, thank you.
0