#1   Report Post  
Posted to microsoft.public.word.docmanagement
bertiep bertiep is offline
external usenet poster
 
Posts: 1
Default 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   Report Post  
Posted to microsoft.public.word.docmanagement
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default 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   Report Post  
Posted to microsoft.public.word.docmanagement
Greg Maxey[_2_] Greg Maxey[_2_] is offline
external usenet poster
 
Posts: 668
Default 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   Report Post  
Posted to microsoft.public.word.docmanagement
Greg Maxey[_2_] Greg Maxey[_2_] is offline
external usenet poster
 
Posts: 668
Default 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

Posting Rules

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing text in Word Asha Microsoft Word Help 1 November 29th 07 12:20 AM
replacing one word with another in a large document handbagposse Microsoft Word Help 1 July 8th 07 03:06 PM
Replacing Tabs in Word unity22 Microsoft Word Help 8 June 12th 07 05:36 PM
Replacing text, instead of replacing, text inserts alongside word Margaret Minker Microsoft Word Help 4 December 9th 05 03:52 PM
How do I stop Word from replacing characters? Scott Atkins Microsoft Word Help 3 November 22nd 05 08:13 AM


All times are GMT +1. The time now is 07:01 AM.

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"