Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.docmanagement
Jan Johansson Jan Johansson is offline
external usenet poster
 
Posts: 1
Default 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   Report Post  
Posted to microsoft.public.word.docmanagement
Greg Maxey Greg Maxey is offline
external usenet poster
 
Posts: 285
Default 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   Report Post  
Posted to microsoft.public.word.docmanagement
cyberdude cyberdude is offline
external usenet poster
 
Posts: 33
Default 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   Report Post  
Posted to microsoft.public.word.docmanagement
Graham Mayor Graham Mayor is offline
external usenet poster
 
Posts: 19,312
Default 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

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Find+Replace Dialog Box, Replace with Format problem Kathy Microsoft Word Help 3 February 1st 08 04:26 AM
Find multiple characters in one find using MSword find/replace Cliff Microsoft Word Help 2 October 29th 06 07:48 PM
Trying to replace words with fields using Find/Replace mbleyle Microsoft Word Help 2 March 29th 06 11:35 PM
Using find and replace or macros to replace page ranges JeremyC Microsoft Word Help 7 February 13th 06 09:20 PM
Find/ Replace is auto-capping the words I want to replace with Graham Mayor Microsoft Word Help 8 January 27th 06 01:39 AM


All times are GMT +1. The time now is 12:38 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"