Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
REPLACING IN WORD
in MsWord, how do i replace the word left for the word right and word right
for the word left in the same document in the same pass? |
#2
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
REPLACING IN WORD
On Wed, 11 Mar 2009 17:40:01 -0700, bertiep
wrote: in MsWord, how do i replace the word left for the word right and word right for the word left in the same document in the same pass? It's impossible to do it in one pass; in fact, it takes three passes to do it properly. Replace left -- %left% right -- left %left% -- right If you leave out the step of temporarily replacing one of the pair with a third value (for which I've shown using % signs, so it won't be confused with anything else), you'll wind up with all of the occurrences saying the same thing. This is a general principle: when you want to swap two values, you must go through a temporary value for one of them. -- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit. |
#3
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
REPLACING IN WORD
I can't swear to it, but I don't think it could be done with the traditional
UI Find and Replace function (not in one pass that is). Word would want to find all "left" and replace with "right" then all the words would be "right" and Word would change them all to "left." You would need to Change all the "left" to some unique sequence then change all the right to some differenct unique sequece then change the first sequence to "right" and the second to "left" You might use a macro like this: Sub ScratchMacro() Dim oRng As Word.Range Set oRng = ActiveDocument.Range With oRng.Find .Text = "left" .Replacement.Text = "*#*" .Execute Replace:=wdReplaceAll .Text = "right" .Replacement.Text = "#*#" .Execute Replace:=wdReplaceAll .Text = "*#*" .Replacement.Text = "right" .Execute Replace:=wdReplaceAll .Text = "#*#" .Replacement.Text = "left" .Execute Replace:=wdReplaceAll End With End Sub Another way would be to process each word one at a time: Sub ScratchMacroII() Dim oWord As Range Dim i As Long Dim bUnTrim As Boolean For i = 1 To ActiveDocument.Words.Count If ActiveDocument.Words(i).Characters.Last = " " Then bUnTrim = True Select Case Trim(ActiveDocument.Words(i)) Case Is = "right" If bUnTrim Then ActiveDocument.Words(i) = "left" & " " Else ActiveDocument.Words(i) = "left" End If Case Is = "Right" If bUnTrim Then ActiveDocument.Words(i) = "Left" & " " Else ActiveDocument.Words(i) = "Left" End If Case Is = "left" If bUnTrim Then ActiveDocument.Words(i) = "right" & " " Else ActiveDocument.Words(i) = "right" End If Case Is = "Left" If bUnTrim Then ActiveDocument.Words(i) = "Right" & ActiveDocument.Words(i).Characters.Last Else ActiveDocument.Words(i) = "Right" End If End Select bUnTrim = False Next i End Sub -- Greg Maxey - Word MVP My web site http://gregmaxey.mvps.org Word MVP web site http://word.mvps.org "bertiep" wrote in message ... in MsWord, how do i replace the word left for the word right and word right for the word left in the same document in the same pass? |
#4
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
REPLACING IN WORD
Jay is right of course. It would only take three find and replace passes.
Not four. - Greg Maxey - Word MVP My web site http://gregmaxey.mvps.org Word MVP web site http://word.mvps.org "Jay Freedman" wrote in message ... On Wed, 11 Mar 2009 17:40:01 -0700, bertiep wrote: in MsWord, how do i replace the word left for the word right and word right for the word left in the same document in the same pass? It's impossible to do it in one pass; in fact, it takes three passes to do it properly. Replace left -- %left% right -- left %left% -- right If you leave out the step of temporarily replacing one of the pair with a third value (for which I've shown using % signs, so it won't be confused with anything else), you'll wind up with all of the occurrences saying the same thing. This is a general principle: when you want to swap two values, you must go through a temporary value for one of them. -- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit. |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Replacing text in Word | Microsoft Word Help | |||
replacing one word with another in a large document | Microsoft Word Help | |||
Replacing Tabs in Word | Microsoft Word Help | |||
Replacing text, instead of replacing, text inserts alongside word | Microsoft Word Help | |||
How do I stop Word from replacing characters? | Microsoft Word Help |