Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-30-2024, 07:44 AM
Jakov93 Jakov93 is offline Duplicate selected words for continuous and individual selection Windows 10 Duplicate selected words for continuous and individual selection Office 2019
Advanced Beginner
Duplicate selected words for continuous and individual selection
 
Join Date: Jul 2021
Posts: 51
Jakov93 is on a distinguished road
Default Duplicate selected words for continuous and individual selection

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
It works fine for continues selection, but not for individual selection, which I mean selecting random word (by using Ctrl) in a sentence then duplicate it by run the macro
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
Reply With Quote
  #2  
Old 09-30-2024, 04:51 PM
macropod's Avatar
macropod macropod is offline Duplicate selected words for continuous and individual selection Windows 10 Duplicate selected words for continuous and individual selection Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,465
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 10-01-2024, 12:36 AM
Jakov93 Jakov93 is offline Duplicate selected words for continuous and individual selection Windows 10 Duplicate selected words for continuous and individual selection Office 2019
Advanced Beginner
Duplicate selected words for continuous and individual selection
 
Join Date: Jul 2021
Posts: 51
Jakov93 is on a distinguished road
Default

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?
Reply With Quote
  #4  
Old 10-01-2024, 12:46 AM
macropod's Avatar
macropod macropod is offline Duplicate selected words for continuous and individual selection Windows 10 Duplicate selected words for continuous and individual selection Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,465
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 10-01-2024, 12:57 AM
Jakov93 Jakov93 is offline Duplicate selected words for continuous and individual selection Windows 10 Duplicate selected words for continuous and individual selection Office 2019
Advanced Beginner
Duplicate selected words for continuous and individual selection
 
Join Date: Jul 2021
Posts: 51
Jakov93 is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 10-13-2024, 01:19 AM
macropod's Avatar
macropod macropod is offline Duplicate selected words for continuous and individual selection Windows 10 Duplicate selected words for continuous and individual selection Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,465
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Duplicate selected words for continuous and individual selection Removing a duplicate words 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
Duplicate selected words for continuous and individual selection VBA to solve duplicate words in one line Bookmaster Word VBA 7 02-03-2015 04:30 PM
Duplicate selected words for continuous and individual selection How to find and delete duplicate words in doc 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

Other Forums: Access Forums

All times are GMT -7. The time now is 10:47 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft