![]() |
#1
|
|||
|
|||
![]()
Hi,
I made a macro to duplicate selected words according to its sequence in a sentence Code:
Sub DuplicateSelectedWords() Dim oRng As range If Len(Selection.range) = 0 Then MsgBox "Select the text first", vbCritical Exit Sub End If Set oRng = Selection.range oRng.Copy oRng.InsertAfter (oRng.text) End Sub For individual selection, it just duplicate only one word, may be the first word or second or the last. So please how to edit the current macro to duplicate all selected words ( individual selection) also. Thanks |
#2
|
||||
|
||||
![]()
Try the following, which works even if you don't have whole words selected and preserves the original formatting:
Code:
Sub DuplicateSelectedWords() Dim Rng As Range With Selection If .Type = wdSelectionIP Then MsgBox "Select the text first", vbCritical Exit Sub End If Set Rng = ActiveDocument.Range(.Words.First.Start, .Words.Last.End) End With With Rng.Duplicate .Collapse wdCollapseEnd .FormattedText = Rng.FormattedText End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Thanks for reply,
Also the same problem To clarify the problem, suppose I have this sentence "I go to school" If I select the entire sentence, it duplicate it effectively But if select "I" , "go" and "school" ----> it duplicate "go" only If I select "I" and "school" ----> it duplicate "school" only So what is the secret behind this problem? |
#4
|
||||
|
||||
![]()
Well, now you're saying you're asking Word to process a set of discontiguous selections. You can't do that with a macro.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
Of course, this is what I mean for individual selection, also I mentioned in my first message "I mean selecting random word (by using Ctrl)"
BTW, thanks for your info Best Regards |
#6
|
||||
|
||||
![]()
Cross-posted at: Duplicate selected words for discontinuous selection - Eileen's Lounge
For cross-posting etiquette, please read: A message to forum cross posters - Excelguru
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
ranjan | Word VBA | 7 | 08-26-2021 02:17 AM |
Page numbers not following. All sections continuous, link to previous selected. | ClaireP | Word | 4 | 07-07-2020 08:12 AM |
![]() |
Bookmaster | Word VBA | 7 | 02-03-2015 04:30 PM |
![]() |
cinvest | Word | 1 | 09-29-2014 08:34 PM |
Why Words doesn’t show the style of the selected words automatically???? | Jamal NUMAN | Word | 0 | 04-14-2011 03:20 PM |