|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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 !!!!! |
#2
|
||||
|
||||
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 |
#3
|
||||
|
||||
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 |
#4
|
|||
|
|||
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). |
#5
|
||||
|
||||
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 |
#6
|
|||
|
|||
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 !!!!!!! |
#7
|
|||
|
|||
OK, I've just changed
Code:
If oSel.Words.Count > 2 Then Code:
If oSel.Words.Count > 1 Then Am I right ? |
#8
|
|||
|
|||
After further testing, this small change is not enough to make the macro work ie on a 3 characters chain...
|
#9
|
||||
|
||||
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 |
#10
|
|||
|
|||
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. |
#11
|
||||
|
||||
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 |
#12
|
|||
|
|||
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. |
#13
|
||||
|
||||
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 |
#14
|
|||
|
|||
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 ! |
#15
|
|||
|
|||
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 |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |
Identify certain text and move all phrases containing it down a line | Chayes | Word VBA | 2 | 11-26-2013 01:16 PM |
How to give line numbering to only selected text? | garlapati | Word | 5 | 04-04-2011 02:05 PM |