PDA

View Full Version : Word macro to find list items and replace them in the rest of the file



mpeterson
03-31-2014, 06:10 AM
I have a big text file that contains a list of items, such as:
a. item number one
b. item number two
c. item number three
d. ..
e. ..
Under that list there is an elaborating text that gives an idea about each item, such as:
At home we need item number one as it helps in cleaning. As for item number two, if you are a handy person you will certainly need it as it protects your hands. In regards to item number three, it is a must to have to protect your children from crawling insects. Etc.

What I am after is a find & replace macro to find the corresponding item from the list in the elaborating text and replaces it with html colour formatting syntax <font color = "red">*the replaced item*</font>, so the listed item will be distinguishable in red when the elaborating text is displayed in the browser. Thus, the previous paragraph will be like the following before it is pasted on the website:
At home we need <font color = "red">item number one</font> as it helps in cleaning. As for <font color = "red">item number two</font>, if you are a handy person you will certainly need it as it protects your hands. In regards to <font color = "red">item number three</font>, it is a must to have to protect your children from crawling insects. Etc.

Can anyone help with this please?
Thanks in advance..

macropod
03-31-2014, 03:34 PM
Try:

Sub BulkUpdate()
Application.ScreenUpdating = False
Dim FRDoc As Document, FRList As String, i As Long
'Load the strings from the reference doc into a text string to be used as an array.
Set FRDoc = Documents.Open("Drive:\FilePath\FindReplaceList.doc")
FRList = FRDoc.Range.Text
FRDoc.Close False
Set FRDoc = Nothing
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWholeWord = True
.MatchCase = True
'Process each word from the List.
For i = 0 To UBound(Split(FRList, vbCr)) - 1
.Text = Split(FRList, vbCr)(i)
.Replacement.Text = " <font color = " & Chr(34) & "red" & Chr(34) & ">^&</font>"
.Execute Replace:=wdReplaceAll
Next
End With
Application.ScreenUpdating = True
End Sub
Change 'Drive:\FilePath\FindReplaceList.doc' to match your requirements.

mpeterson
03-31-2014, 05:33 PM
Thank you very much Paul for your promptness and sincere cooperation. I ran the code after changing the file path to "d:\test.doc" and actually it stopped at ".Text" line in "For" loop. Pardon me, I'm not anything in programming, but I thought the stopping may be because the code did not recognise or find each list item since it is a plain text file and each list item identifier is a text and is a part of the line, ie "a. item number one" where "a." is a text, accordingly the list will not be formed correctly. So what is needed is to select only the text "item number one" without "a." to "item number 'n'" and replace their corresponding texts in the following paragraph with the html formatting statement.

I would like also to add that the text file has hundreds of lists each list is followed by an elaborating paragraph, and items in each list are unique and always between "a." and "z.". So the structure of the file is very close to the following:

a. item number one
b. item number two
c. item number three

A paragraph about item number one, item number two, item number three.

a. Another item number one
b. Another item number two
c. Another item number three
d. Another item number four
e. Another item number five

A paragraph elaborating all five items above.

and so on..

If this is too complicated to compose a script to run on the entire file of such structure, I am ready to cut and past each list with its paragraph and run the script that will do the replacement, but I still hope there is a way to do it all in one go.

Thank you Paul once again, and have a good day mate : )

macropod
03-31-2014, 05:52 PM
So, your text file has the 'a. ', 'b. ', etc at the start of each line, but they're not to be part of the find expressions?

Also, please clarify about the files being processed - are the lists in one file and the text to be updated in another, or are they both in the same file? If it's the latter, quite different code will be required and there would have to be an unambiguous way for the code to differentiate between the lists and the content they're to update.

mpeterson
03-31-2014, 06:24 PM
Thanks Paul. Yes, 'a.', 'b.' etc are at the start of each list item per line, and they are not part of the find expression. There's also a 'space" after the dot before the start of the find expression, so it is 'a. item to be found'.

Lists and their paragraphs are in the same file (unfortunately). This is why, in order to simplify the solution, I suggested that I run the code manually per 'piece', ie the list and its paragraph, by copying and pasting it into the active document where the code can run. It will take a long time to finish, but compared to reformatting each list item manually.. this will take months to finish. The most important part in the code is that it finds the exact list item without its identifier with a space eg (a. ), and replaces it by including it in the html formatting syntax, so the paragraph will have items names in red.

macropod
04-01-2014, 12:16 AM
In that case, open the file and try:

Sub AddTags()
Application.ScreenUpdating = False
Dim Rng As Range, StrFnd As String
With ActiveDocument.Range
.InsertBefore vbCr
Set Rng = .Characters.First
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^13[a-z]. *^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
Rng.End = .Duplicate.End
With Rng
If .Paragraphs.Count = 2 Then
.MoveStartUntil " ", wdForward
.Start = .Start + 1
StrFnd = StrFnd & .Text
.End = .End - 1
.Collapse wdCollapseEnd
Else
.End = .Paragraphs.Last.Range.Start - 1
Call RngTag(Rng, StrFnd)
.Collapse wdCollapseEnd
StrFnd = ""
End If
End With
.Start = Rng.End
.Collapse wdCollapseStart
.Find.Execute
Loop
If StrFnd <> "" Then
Rng.End = .End
Call RngTag(Rng, StrFnd)
End If
End With
ActiveDocument.Characters.First.Delete
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
'
Sub RngTag(Rng As Range, StrFnd As String)
Dim i As Long
With Rng
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
.MatchPhrase = True
For i = 0 To UBound(Split(StrFnd, vbCr)) - 1
.Text = Split(StrFnd, vbCr)(i)
.Replacement.Text = " <font color = " & Chr(34) & "red" & Chr(34) & ">^&</font>"
.Execute Replace:=wdReplaceAll
Next
End With
End With
End Sub

mpeterson
04-01-2014, 02:16 AM
Thank you Paul a lot for all your effort and time to produce such script.
I had a go with the code on following simple "piece" of text in an active Word document where I placed the code:

a. item number one
b. item number two
c. item number three

Under that list there is an elaborating text that gives an idea about each item, such as:
At home we need item number one as it helps in cleaning. As for item number two, if you are a handy person you will certainly need it as it protects your hands. In regards to item number three, it is a must to have to protect your children from crawling insects. Etc.

I think I missed out on something as I found no change occurred to "item number one, or two or three" in the paragraph. What mistake do you think I made to get such result?

My best regards.

macropod
04-01-2014, 03:54 AM
I tested it with that text (from your first post) before posting the latest code. All I can conclude is that either the content differs from what you've described, or you didn't run the macro on that data.

mpeterson
04-01-2014, 04:19 AM
Yes Paul, you're right. My test was on a copy of my example but not as plain text, so the code didn't parse it. Copying it and pasting it as plain text, the code worked as perfectly as it is meant to. Thanking you is still very little for what you did for me, but I still thank you very much for everything.

All the best Paul.

zumauma
06-05-2014, 12:56 AM
Hello,

I have a similar request, so it would be highly appreciated if someone could help me.

I have two documents (the main one, in which the modifications need to be made) and another one (the checklist, containing a list of file paths that look something like this <1234> \folder\file.jar\file.txt). What I would like to do, is to have a macro, that would compare the file paths in the checklist with the ones in the main one, and once file paths found to highlight them in a whatever color.

Thank you!
Alina

macropod
06-05-2014, 04:58 PM
Simply change:
.Replacement.Text = " <font color = " & Chr(34) & "red" & Chr(34) & ">^&</font>"
to:
.Replacement.Text = "^&"
and, after it, insert:
.Replacement.Highlight = True