Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-28-2018, 02:35 AM
gloub gloub is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 7 64bit How to move a line to another line that starts with a chain selected in the 1st one? Office 2003
Novice
How to move a line to another line that starts with a chain selected in the 1st one?
 
Join Date: Feb 2018
Location: Paris, France
Posts: 29
gloub is on a distinguished road
Default How to move a line to another line that starts with a chain selected in the 1st one?

Hello,



I am new to this forum, not very experienced in Word and I don't know anything about VBA but I think this is where I should ask my question.

I use Word to make a to-do list.
To improve it, I'd need to be able to automatically move the current line (I would call it L1) to the next line that looks like it (L2).
More precisely, once you have selected a chain in L1, find the next line that starts with the same text, and move L1 just before L2.
But I don't know how to do that.
Could a crack help me?

Thanks !!!!!
Reply With Quote
  #2  
Old 10-28-2018, 03:40 AM
gmayor's Avatar
gmayor gmayor is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 10 How to move a line to another line that starts with a chain selected in the 1st one? Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

In order to do this, it would be necessary to know that by 'line' you meant 'paragraph' and not the displayed 'line' on the page. i.e. the 'line' would need to end with a paragraph break '¶' as shown when formatting is displayed.


It would also be necessary to know what you mean by 'starts with the same text'. To what extent is the text the same?
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 10-28-2018, 03:51 AM
Guessed's Avatar
Guessed Guessed is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 10 How to move a line to another line that starts with a chain selected in the 1st one? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

If you selected the paragraphs and sorted them alphabetically, would that be close enough?

You can add the sort button by customizing the Quick Access Toolbar to add this command.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 10-28-2018, 04:39 AM
gloub gloub is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 7 64bit How to move a line to another line that starts with a chain selected in the 1st one? Office 2003
Novice
How to move a line to another line that starts with a chain selected in the 1st one?
 
Join Date: Feb 2018
Location: Paris, France
Posts: 29
gloub is on a distinguished road
Default

Thanks to both of you.

@gmayor
So, yes, by "line" I mean "paragraph".
And when I say 'starts with the same text' I mean that the second paragraph (L2, which I should now call P2) must start with exactely the same chain than the one selected in L1 before executing the macro.

@Guessed
No, sorting paragraphs wouldn't be convenient, as I don't want to change the order of the other paragraphs (in my to-do list, the order is significant, so after sorting paragraphs I would lose informations).
Reply With Quote
  #5  
Old 10-28-2018, 05:17 AM
gmayor's Avatar
gmayor gmayor is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 10 How to move a line to another line that starts with a chain selected in the 1st one? Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The following macro will find the next paragraph that begins with the selected text and will insert the paragraph that contains the selected text before it. It assumes that at least two words are selected. You can change that as appropriate. http://www.gmayor.com/installing_macro.htm

Code:
Sub Macro1()
'Graham Mayor - http://www.gmayor.com - Last updated - 28 Oct 2018 

 Dim oRng As Range, oSel As Range
    Set oSel = Selection.Range
    If oSel.Words.Count > 2 Then
        Set oRng = ActiveDocument.Range
        oRng.Start = oSel.Paragraphs(1).Range.End
        With oRng.Find
            Do While .Execute(oSel)
                If oRng.Start = oRng.Paragraphs(1).Range.Start Then
                    oRng.Collapse 1
                    oRng.FormattedText = oSel.Paragraphs(1).Range.FormattedText
                    oSel.Paragraphs(1).Range.Delete
                    Exit Do
                End If
                oRng.Collapse 0
            Loop
        End With
    End If
lbl_Exit:
    Set oRng = Nothing
    Set oSel = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #6  
Old 10-28-2018, 05:44 AM
gloub gloub is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 7 64bit How to move a line to another line that starts with a chain selected in the 1st one? Office 2003
Novice
How to move a line to another line that starts with a chain selected in the 1st one?
 
Join Date: Feb 2018
Location: Paris, France
Posts: 29
gloub is on a distinguished road
Default

WOW !!!!

That's great, thanks : it's 99% what I was dreaming of !

The last thing is : I need the macro to work on a chain, no matter whether it is 3 characters or a whole sentence (typically I would select 4 or 5 characters).
Could you help me do this, please ?

THANKS !!!!!!!
Reply With Quote
  #7  
Old 10-28-2018, 06:09 AM
gloub gloub is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 7 64bit How to move a line to another line that starts with a chain selected in the 1st one? Office 2003
Novice
How to move a line to another line that starts with a chain selected in the 1st one?
 
Join Date: Feb 2018
Location: Paris, France
Posts: 29
gloub is on a distinguished road
Default

OK, I've just changed
Code:
If oSel.Words.Count > 2 Then
to
Code:
If oSel.Words.Count > 1 Then
and it seems to work perfectely.
Am I right ?
Reply With Quote
  #8  
Old 10-28-2018, 06:25 AM
gloub gloub is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 7 64bit How to move a line to another line that starts with a chain selected in the 1st one? Office 2003
Novice
How to move a line to another line that starts with a chain selected in the 1st one?
 
Join Date: Feb 2018
Location: Paris, France
Posts: 29
gloub is on a distinguished road
Default

After further testing, this small change is not enough to make the macro work ie on a 3 characters chain...
Reply With Quote
  #9  
Old 10-28-2018, 08:53 PM
gmayor's Avatar
gmayor gmayor is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 10 How to move a line to another line that starts with a chain selected in the 1st one? Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Use the following instead
Code:
If Len(oSel) > 2 Then
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #10  
Old 10-29-2018, 03:14 PM
gloub gloub is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 7 64bit How to move a line to another line that starts with a chain selected in the 1st one? Office 2003
Novice
How to move a line to another line that starts with a chain selected in the 1st one?
 
Join Date: Feb 2018
Location: Paris, France
Posts: 29
gloub is on a distinguished road
Default

Hi!
Thanks for your answer.

I've made that change.
Now the macro works on chains (not only words), but chains have to contain at least 5 characters, and in rare cases, it doesn't work...
I couldn't figure out why.
Reply With Quote
  #11  
Old 10-29-2018, 04:10 PM
Guessed's Avatar
Guessed Guessed is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 10 How to move a line to another line that starts with a chain selected in the 1st one? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

It most likely doesn't work when the paragraphs don't start with EXACTLY the same content. For instance, if there is a space at the start on one para and not on the other then that won't be registered as a hit.

You would need to post a sample doc to allow someone to look at your actual content to verify the exact cause of the issue you are seeing.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #12  
Old 11-04-2018, 04:02 AM
gloub gloub is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 7 64bit How to move a line to another line that starts with a chain selected in the 1st one? Office 2003
Novice
How to move a line to another line that starts with a chain selected in the 1st one?
 
Join Date: Feb 2018
Location: Paris, France
Posts: 29
gloub is on a distinguished road
Default

Hi,

Sorry for answering a bit late, I was away for a few days.

So, as you asked, I post a file to make thigs clearer (sorry, I had to anonymise data, that's why texts looks so weird).

Basically, I'd like the macro to do this :
After selecting a chain (in red in my file), the whole paragraph (in yellow) will move just before the next paragraph starting with the same red chain (like the 2nd paragraph in yellow with the same red chain at the beginning).

The macro works fine on the yellow paragraphs, but doesn't on the green ones.
I can't figure out why...

Does anyone have any ideas?
Thanks.
Attached Files
File Type: doc test.doc (486.0 KB, 10 views)
Reply With Quote
  #13  
Old 11-04-2018, 04:36 PM
Guessed's Avatar
Guessed Guessed is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 10 How to move a line to another line that starts with a chain selected in the 1st one? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

The first GAL is followed by a non-breaking space and the second one has a regular space. If you are selecting the initial word and including the trailing space then that would be an obvious cause for the macro to 'miss' the next instance.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #14  
Old 11-04-2018, 05:01 PM
gloub gloub is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 7 64bit How to move a line to another line that starts with a chain selected in the 1st one? Office 2003
Novice
How to move a line to another line that starts with a chain selected in the 1st one?
 
Join Date: Feb 2018
Location: Paris, France
Posts: 29
gloub is on a distinguished road
Default

Brillant !
I don't know how this non-breaking space got there but I'll immediately turn it into a regular one.
Thank you so much !
Reply With Quote
  #15  
Old 11-11-2018, 12:06 AM
gloub gloub is offline How to move a line to another line that starts with a chain selected in the 1st one? Windows 7 64bit How to move a line to another line that starts with a chain selected in the 1st one? Office 2003
Novice
How to move a line to another line that starts with a chain selected in the 1st one?
 
Join Date: Feb 2018
Location: Paris, France
Posts: 29
gloub is on a distinguished road
Default

One more point (on a different matter).
One thing impresses me in your code : you manage to move a paragraph with no change in the page display.

I've made lots of macros to move paragraphs from one place to another in my todolist.
The only method I've found (due to my lack of knowledge in VBA) is to insert a chain (say "µµµµ") then move the paragraph, then move back to the chain, then delete it, which may make unpleasant display changes.
Is there a way to improve this archaic method to get the same result than in you macro : move a paragraph while the display remains unchanged ?

Many thanks !

HTML Code:
Sub Move_To_Next_Monday()
' this macro finds the next paragraph starting with "~Monday" then moves the selected chain one line further then goes back to where the selected text was at the beginning

    Application.ScreenUpdating = False

    Selection.HomeKey Unit:=wdLine
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.Cut
    Selection.TypeText Text:="µµµµ"
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "~Monday"
        .Replacement.Text = ""
        .Forward = True
    End With
    Selection.Find.Execute
    
    Selection.HomeKey Unit:=wdLine
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.Paste
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "µµµµ"
        .Replacement.Text = ""
        .Forward = True
    End With
    Selection.Find.Execute
    
    Selection.Delete Unit:=wdCharacter, Count:=1
End Sub
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to move a line to another line that starts with a chain selected in the 1st one? Document starts to red line spaces and formatting appears on right side Richtriebe Word 2 03-23-2017 11:53 AM
Bold each line of text that starts with a recurring symbol qubie Word 6 08-26-2016 07:10 AM
Lock line so text does not move to next line saundrals Word 2 06-19-2014 03:59 PM
How to move a line to another line that starts with a chain selected in the 1st one? Identify certain text and move all phrases containing it down a line Chayes Word VBA 2 11-26-2013 01:16 PM
How to move a line to another line that starts with a chain selected in the 1st one? How to give line numbering to only selected text? garlapati Word 5 04-04-2011 02:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:28 PM.


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