#1
|
|||
|
|||
I need a macro to move dangling words to a new line please
Problem
I would like to use a macro to check a document in full and identify dangling words at the end of a line, e.g: This is an example of the dangling word problem. This is the dangling word. And move the dangling word to the next line, e.g: This is an example of the dangling word problem. This is the dangling word. TIA |
#2
|
|||
|
|||
For something like your specific example where it appears that you have running text this "MIGHT" work fundamentally. The document needs to be in print layout view. Basically, it loops through the Page, Rectangle and Line objects and sees "." " " and "This" as the Previous.Previous, Previous and Last word of the line. In that condition, the Previoius word (or the space between the "." and This) is replaced with a line break forcing "This" to the next line."
Code:
Sub LoopByLine() Dim oPage As Page Dim lngRec As Long, lngLine As Long Dim oRng As Word.Range For Each oPage In ActiveDocument.ActiveWindow.ActivePane.Pages For lngRec = 1 To oPage.Rectangles.Count For lngLine = 1 To oPage.Rectangles(lngRec).Lines.Count Set oRng = oPage.Rectangles(lngRec).Lines(lngLine).Range If oRng.Words.Last.Previous.Previous = "." Then oRng.Words.Last.Previous = Chr(11) End If Application.ScreenRefresh Next lngLine Next lngRec Next oPage lbl_Exit: Exit Sub End Sub However, in practice and considering that the result you see on your PC could be vastly different than what I would see on my PC, it is probably a futile exercise. Whatever you do, save and publish as a PDF file before distribution. |
#3
|
||||
|
||||
One might approach it via a wildcard Find/Replace, where:
Find = ([.:;\!\?] <*>)^32 Replace = \1^s This way, the 'dangling word' problem will be resolved for whatever system the document is viewed on, even if the page layout changes. No code required.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
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? | gloub | Word VBA | 24 | 03-19-2019 03:12 PM |
Capitalize one to two words if they are the only words on a line | jrhlavac | Word VBA | 1 | 10-08-2015 08:19 PM |
macro to add brackets to each line and add single quotes to each word in the line | bracketandquotes | Word VBA | 17 | 02-16-2015 03:51 PM |
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 |