Swap two selections

A

Andy

What would the code be to swap two marked selections? So if I had
text that said "red or blue", and I selected both "red" and "blue" and
ran the macro, the text would then read "blue or red"?

I need to do this sort of thing often enough so a macro would be
helpful, but I'm also very curious to see how one would go about doing
this.

TIA,

Andy
 
D

Doug Robbins

As long as the selection includes the space after the third word, the
following will swap the first and third words:

Dim A As String, B As String, C As String
A = Selection.Range.Words(1)
B = Selection.Range.Words(2)
C = Selection.Range.Words(3)
Selection.Range.Text = C & B & A

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
A

Andy

As long as the selection includes the space after the third word, the
following will swap the first and third words:

Dim A As String, B As String, C As String
A = Selection.Range.Words(1)
B = Selection.Range.Words(2)
C = Selection.Range.Words(3)
Selection.Range.Text = C & B & A


Doug,

Thanks, that answers part of my question, in terms of how one would go
about this sort of thing.

But what if I have two selections, not necessarily word 1 and word 3,
and I wanted to swap those? How would I identify the first selection
and the second selection?

Andy
 
D

Doug Robbins

The following will swap the first and the last selected words

Dim A As String, B As String, i As Long
With Selection.Range
i = .Words.Count
A = .Words(1)
B = .Words(i)
.Words(1).Select
End With
Selection.Range.Text = B
Selection.MoveRight wdWord, i - 1
Selection.Words(1).Select
Selection.Range.Text = A


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
A

Andy

Dim A As String, B As String, i As Long
With Selection.Range
i = .Words.Count
A = .Words(1)
B = .Words(i)
.Words(1).Select
End With
Selection.Range.Text = B
Selection.MoveRight wdWord, i - 1
Selection.Words(1).Select
Selection.Range.Text = A

Doug,

It doesn't quite work. I select two separate words, using Ctrl-click,
and when I step through the macro looking in the Locals Window, the
second word is shown both as String A and String B. Not sure what I'm
doing wrong.

Thanks for bothering with this.


Andy
 
J

Jay Freedman

Hi Andy,

The problem isn't so much with Doug's macro, or anything you're doing
wrong, as it is with Word's almost complete lack of support in VBA for
multiple simultaneous selections (what the documentation refers to as
"discontiguous selection"). This is discussed at
http://support.microsoft.com/kb/q288424. Nothing has changed in this
regard from Word 2002 to Word 2003.
 
A

Andy

The problem isn't so much with Doug's macro, or anything you're doing
wrong, as it is with Word's almost complete lack of support in VBA for
multiple simultaneous selections (what the documentation refers to as
"discontiguous selection"). This is discussed at
http://support.microsoft.com/kb/q288424. Nothing has changed in this
regard from Word 2002 to Word 2003.

Interesting article. I think I'll give up for now <g>.
 
A

Andy

As long as the selection includes the space after the third word,
the following will swap the first and third words:

Dim A As String, B As String, C As String
A = Selection.Range.Words(1)
B = Selection.Range.Words(2)
C = Selection.Range.Words(3)
Selection.Range.Text = C & B & A

Doug,

Even though I sometimes want to swap words other than the first and
third, this is what I end up needing to do often enough that this
macro is quite useful for me.

Except sometimes the last word is followed by a comma or a period.

If the selection goes only to the end of the third word, not including
the following space, comma, period, or whatever, what would the code
be to temporarily append a space to the selection, run the macro, and
then delete the trailing space? Everything I try either appends the
space too late, or doesn't leave the result properly spaced.

Besides helping me with my work in Word, this is also very useful in
increasing my meager VBA knowledge.

Thanks,


Andy
 
D

Doug Robbins

If you do not include the space after the third word in the selection, then
replace the last line of the macro with

Selection.Range.Text = C & " " & B & Trim(A)
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
A

Andy

If you do not include the space after the third word in the selection, then
replace the last line of the macro with

Selection.Range.Text = C & " " & B & Trim(A)

Completely helps Doug, thanks!

I was going at this the wrong way, no need to add the last space
to make the macro work.

I just looked up Trim, LTrim, and RTrim.

I'm starting to understand a bit about string concatenation, and how
to add text (I should have been able to figure out C & " " on my own).

How does one go about removing text, other than trailing spaces? Say
for example changing "however," to "however"? What if you just want
to remove the last character in a string but don't know what it is?
What if you want to remove all instances of a particular character or
combination of characters?

Is there a link you could point me to that would discuss this sort of
thing? Or the pertinent Help Topic? It's sometimes hard to find what
you are looking for in VBA Help.


Andy
 
D

Doug Robbins

Check out Left(), Right() and Mid(). Also while you are at it InStr()

You should probably also take a look at "Finding and replacing characters
using wildcards" at:

http://word.mvps.org/FAQs/General/UsingWildcards.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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