macro help please

D

dendenden

Hello all this is my first post. I could do with your advice.

I have a 20 similar documents which share many repeating comments of 1
sentence long. I have to update these old comments with new edited comments.

I have created a table with two columns, containing the old comments and the
corresponding new comments that I have edited. I plan to make a macro in
Msword to find and replace the old comments for new, then run the macro on
all 20 documents.

However the process of creating the macro in itself is likely to take
considerable time due to the number of comments (approx 250- 500 in total).

And here’s the question: Is there a way to make the macro use the table to
change the old comments for the new? Or is there an easier way to do this?

Any help would be greatly appreciated. If you require any other
clarifications, please let me know, thanks in advance, Den.
 
J

Jonathan West

Hi den


dendenden said:
Hello all this is my first post. I could do with your advice.

I have a 20 similar documents which share many repeating comments of 1
sentence long. I have to update these old comments with new edited
comments.

I have created a table with two columns, containing the old comments and
the
corresponding new comments that I have edited. I plan to make a macro in
Msword to find and replace the old comments for new, then run the macro on
all 20 documents.

The following article may help you

Find & ReplaceAll on a batch of documents in the same folder
http://www.word.mvps.org/FAQs/MacrosVBA/BatchFR.htm
However the process of creating the macro in itself is likely to take
considerable time due to the number of comments (approx 250- 500 in
total).

And here's the question: Is there a way to make the macro use the table to
change the old comments for the new? Or is there an easier way to do this?

Yes. The simplest approach is probably to load all the table's contents into
an array. Something like this

Dim sText() as String
Dim iRow as Long
Dim iColumn as Long
With ActiveDocument.Tables(1)
ReDim sText(1 to .Rows.Count, 1 To .Columns.Count)
For iRow = 1 to .Rows.Count
For iColumn = 1 To .Columns.Count
sText(iRow, iColumn) = .Cell(iRow, iColumn).Range.Text
sText(iRow, iColumn) = Left$(sText(iRow, iColumn),
Len(sText(iRow, iColumn)) - 2)
Next iColumn
Next iRow
End With

Now, you have all the strings of the table neatly arranged in a (presumably
2-column) array. What you now do is open each file in turn and do a
find/replace on each string pair. Your macro should continue like this.

Dim oDoc as Document
Dim vFile as Variant
With Application.FileSearch
.Lookin = "C:\Test" 'put your folder here
.FileName = "*.doc"
If .Execute Then
For Each vFile in .FoundFiles
Set oDoc = Documents.Open(vFile)
With oDoc.Range.Find
.Format = False
For iRow = 1 to UBound(sText, 1)
.Text = sText(iRow, 1)
.Replacement.Text = sText(iRow, 2)
.Execute Replace:=wdReplaceAll
Next iRow
End With
oDoc.Save
oDoc.Close
Next vFile
End If
End With
 
J

Jean-Guy Marcil

dendenden said:
Hello all this is my first post. I could do with your advice.

I have a 20 similar documents which share many repeating comments of 1
sentence long. I have to update these old comments with new edited comments.

I have created a table with two columns, containing the old comments and the
corresponding new comments that I have edited. I plan to make a macro in
Msword to find and replace the old comments for new, then run the macro on
all 20 documents.

However the process of creating the macro in itself is likely to take
considerable time due to the number of comments (approx 250- 500 in total).

And here’s the question: Is there a way to make the macro use the table to
change the old comments for the new? Or is there an easier way to do this?

Any help would be greatly appreciated. If you require any other
clarifications, please let me know, thanks in advance, Den.

See the following page for code to do a batch find/replace:
http://word.mvps.org/faqs/macrosvba/BatchFR.htm

Place this code in the document containing the two-colum table.

Adapt the code so that it uses a loop to do the find replace:

For i = 1 to ActiveDocument.Tables(1).Rows.Count

....Find.Text = ActiveDocument.Tables(1).Cells(i,1).Range.Text
....Replace.Text = ActiveDocument.Tables(1).Cells(i,2).Range.Text

Next

Etc.

You will probably need to adjust the code to remove the cell marker from the
string:
strFind = ActiveDocument.Tables(1).Cells(i,1).Range.Text
....Find.Text = Left(strFind, Len(strFind) -2)
Etc.

Bear in mind that doing up to 500 find/replace in 20 documents might take a
little while...
 
D

dendenden

Thanks very much for time jean-guy and Jonathan- I will give it a go- i knew
there must have been a way around it.

cheers once again, den
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top