Log in

View Full Version : [SOLVED:] Search for single word or character lines in a paragraph



janaboo13
08-08-2012, 10:06 AM
I'm trying to search for paragraphs in Word that have only one character (Asian languages) in the last line of the paragraph and then insert a line break before the character.

I recorded this macro that finds a paragraph mark, moves the cursor to the left and inserts a line break. I tested it on only one paragraph so it's missing some loop code, I'm sure.

Sub Macro1()
'
' Macro1 Macro
'
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.CorrectHangulEndings = True
.HanjaPhoneticHangul = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=4
Selection.TypeText Text:=Chr(11)
End Sub

I now need to know the code, if it's possible, to only find those paragraph marks for paragraphs that have only one character in the last line of the paragraph. Is that possible?

Any help would be appreciated!
Jan

macropod
08-08-2012, 03:01 PM
Hi Jan,

There is a significant potential problem with what you're asking in that Word uses information returned by the active printer's driver to lay out a document. This means that what might be a single-word line on one PC might actually part of a multi-word line on another (or even on the same PC when attached to a different printer). Consequently, any attempt to do what you're asking (though possible) is not going to produce valid results for all configurations that the processed document might be viewed on.

Given the above, are you sure you want to proceed?

fumei
08-08-2012, 06:09 PM
Why is there a character count of 4 if there is only one character before the paragraph mark?

janaboo13
08-09-2012, 06:18 AM
Fumei,

I'm working with Chinese at the moment and tested my little macro on that language and document. To record, I searched for a paragraph mark, then I clicked in the document and moved the cursor to the left so the insertion point was before the second to last character in the last sentence of the paragraph. I did this in record mode and that's the number that was recorded. I can only assume it counted the paragraph mark, period, and then two characters.

Is there a better way to do this?

Thank you so much, Jan

janaboo13
08-09-2012, 06:32 AM
Hi Jan,

There is a significant potential problem with what you're asking in that Word uses information returned by the active printer's driver to lay out a document. This means that what might be a single-word line on one PC might actually part of a multi-word line on another (or even on the same PC when attached to a different printer). Consequently, any attempt to do what you're asking (though possible) is not going to produce valid results for all configurations that the processed document might be viewed on.

Given the above, are you sure you want to proceed?

Well, given that the only other way to do this is to run the macro on all paragraphs (docs are from 60 to 250 pgs long) with a message box asking is you want to insert the line break, so I'd sure like to try.

We're publishing from a database and the template that is used for publishing these docs is attached no matter who opens/publishes the docs. Only one person publishes to Word (on their local machine) and when done, a PDF is created. No Word docs are routed or shared.

After yesterday, I also realized that I will only need to run this on Chinese and Japanese documents, since these are the only two languages that we translate into that don't have words separated by spaces.

I spent quite a bit of time reading and looking for examples yesterday, so I gathered this might be challenging. I'd love to learn more if it is at all possible. If is isn't, then I understand. OR, is there another solution that might be less time consuming than this (with the same results) and require little/no intervention from the user?

I sure appreciate any help.

Thanks so much! Jan