Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2005

Tip: Looking for answers? Try searching our database.

Swap two selections

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andy - 09 Jul 2005 17:07 GMT
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

Doug Robbins - 09 Jul 2005 18:05 GMT
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

Signature

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

> 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
[quoted text clipped - 7 lines]
>
> Andy
Andy - 09 Jul 2005 22:36 GMT
> As long as the selection includes the space after the third word, the
> following will swap the first and third words:
[quoted text clipped - 4 lines]
> 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


Doug Robbins - 10 Jul 2005 08:25 GMT
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

Signature

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

>> As long as the selection includes the space after the third word, the
>> following will swap the first and third words:
[quoted text clipped - 15 lines]
>
> Andy
Andy - 10 Jul 2005 15:30 GMT
  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


Jay Freedman - 10 Jul 2005 17:31 GMT
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.

--
Regards,
Jay Freedman
Microsoft Word MVP         FAQ: http://word.mvps.org

>   Dim A As String, B As String, i As Long
>   With Selection.Range
[quoted text clipped - 18 lines]
>
>Andy
Andy - 10 Jul 2005 18:11 GMT
> 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>.
Andy - 11 Jul 2005 21:03 GMT
 > 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
Doug Robbins - 12 Jul 2005 10:20 GMT
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)
Signature

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

>  > As long as the selection includes the space after the third word,
>  > the following will swap the first and third words:
[quoted text clipped - 25 lines]
>
> Andy
Andy - 13 Jul 2005 02:27 GMT
> 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
Doug Robbins - 13 Jul 2005 15:54 GMT
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

Signature

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

>> If you do not include the space after the third word in the selection,
>> then
[quoted text clipped - 23 lines]
>
> Andy
Andy - 14 Jul 2005 01:29 GMT
> 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

Thanks Doug!

Andy

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.