Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
Using macros to find and replace
I am doing a lot of translation, and sometimes it is helpful to conduct a global search and replace words. I have now been told that it is possible to write a macro which can find and replace several words in one go. (today I am doing it word by word) Do you have any idea on how to do this? Jan J. -- Jan Johansson |
#2
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
Using macros to find and replace
See:
http://gregmaxey.mvps.org/VBA_Find_And_Replace.htm -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Greg Maxey - Word MVP My web site http://gregmaxey.mvps.org Word MVP web site http://word.mvps.org ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "Jan Johansson" wrote in message .. . I am doing a lot of translation, and sometimes it is helpful to conduct a global search and replace words. I have now been told that it is possible to write a macro which can find and replace several words in one go. (today I am doing it word by word) Do you have any idea on how to do this? Jan J. -- Jan Johansson |
#3
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
Using macros to find and replace
On 3$B7n(B3$BF|(B, $B28a(B6$B;~(B54$BJ,(B, Jan Johansson Jan.Johansson.
wrote: I am doing a lot of translation, and sometimes it is helpful to conduct a global search and replace words. I have now been told that it is possible to write a macro which can find and replace several words in one go. (today I am doing it word by word) Do you have any idea on how to do this? Jan J. -- Jan Johansson Can Word's Find_and_replace also help? (press Ctrl-H to activate the function) Mike |
#4
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
Using macros to find and replace
cyberdude wrote:
Can Word's Find_and_replace also help? (press Ctrl-H to activate the function) Mike Find and replace will not work from a list of Words, though the replace function will certainly work from lists if called from a macro. Greg's solution is probably simplest as he has done all the work, but the following macro uses a two column table saved in the document - sFname = "D:\My Documents\Test\changes.doc" and replaces the words in the current document from the first column with those in the second Sub ReplaceFromTableList() Dim ChangeDoc As Document, RefDoc As Document Dim cTable As Table Dim oldPart As Range, newPart As Range Dim i As Long Dim sFname As String sFname = "D:\My Documents\Test\changes.doc" Set RefDoc = ActiveDocument Set ChangeDoc = Documents.Open(sFname) Set cTable = ChangeDoc.Tables(1) RefDoc.Activate For i = 1 To cTable.Rows.Count Set oldPart = cTable.Cell(i, 1).Range oldPart.End = oldPart.End - 1 Set newPart = cTable.Cell(i, 2).Range newPart.End = newPart.End - 1 With Selection .HomeKey wdStory With .Find .ClearFormatting .Replacement.ClearFormatting .Execute findText:=oldPart, _ ReplaceWith:=newPart, _ Replace:=wdReplaceAll, _ MatchWildcards:=False, _ Forward:=True, _ Wrap:=wdFindContinue End With End With Next i ChangeDoc.Close wdDoNotSaveChanges End Sub or you could use arrays, which is quicker, though not as convenient for long lists eg Sub ReplaceList() Dim vFindText As Variant Dim vReplText As Variant Dim i As Long vFindText = Array(Chr(33), "Lorem", "ipsum", "dolor") vReplText = Array(Chr(46), "WORD1", "WORD2", "WORD3") With Selection .HomeKey wdStory With .Find .ClearFormatting .Replacement.ClearFormatting .Forward = True .Wrap = wdFindContinue .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Format = True .MatchCase = True For i = LBound(vFindText) To UBound(vFindText) .Text = vFindText(i) .Replacement.Text = vReplText(i) .Execute Replace:=wdReplaceAll Next i End With End With End Sub -- Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Find+Replace Dialog Box, Replace with Format problem | Microsoft Word Help | |||
Find multiple characters in one find using MSword find/replace | Microsoft Word Help | |||
Trying to replace words with fields using Find/Replace | Microsoft Word Help | |||
Using find and replace or macros to replace page ranges | Microsoft Word Help | |||
Find/ Replace is auto-capping the words I want to replace with | Microsoft Word Help |