Is word at end of the line

J

jlhamann

When I seach for a word with the code:

Selection.Find.ClearFormatting
With Selection.Find
.Text = " on"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

Is there a way to determinine is the word is the last word on that line? I
want to certain words to be moved to the next line if they are at the end of
a line.
 
T

Tony Jollans

There is nothing you can really do about end of line per se - it is not a
fixed position and depends on the printer.

If what you actually have is paragraph marks and/or manual line breaks
explicitly causing the line breaks I would strongly suggest you avoid the
practice and leave Word to flow the text to fit the page, but you could
search for them immediately following the word - ^p will find paragraph
marks and ^l will find line breaks
 
J

jlhamann

I agree, normally I would let Word flow the text, but in some languages it is
grammatically incorrect to have certain words at the end of a line. They
need to go with the next word after them. Word does not always flow the text
correctly. I don’t want to go though long documents looking for these
situations. I would like to have a macro that will find them and move the
word to the next line.
 
H

Helmut Weber

Hi,

as this is a programmers group,
i'm assuming, you can adjust the
following to your needs.

It is just to you give you an idea
of what could be done.

But keep in mind Tony's remark.

Sub Test100a()
Dim rDcm As Range
Dim rTmp As Range
Dim lTmp As Long
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "over "
.MatchWholeWord = True
While .Execute
rDcm.Select
If rDcm.Bookmarks("\line").Range.Words.Last = "over " Then
Set rTmp = rDcm.Bookmarks("\line").Range.Words.Last
rTmp.Select ' for testing
lTmp = MsgBox("move?", vbYesNo)
If lTmp = vbYes Then
rTmp.InsertBefore Chr(11)
End If
End If
Wend
End With
End Sub

Sub Test100b()
Dim rDcm As Range
Dim rTmp As Range
Dim lTmp As Long
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "over "
.MatchWholeWord = True
While .Execute
rDcm.Select
If rDcm.Bookmarks("\line").Range.Words.Last = "over " Then
Set rTmp = rDcm.Bookmarks("\line").Range.Words.Last
rTmp.Select ' for testing
lTmp = MsgBox("move?", vbYesNo)
If lTmp = vbYes Then
rTmp.Characters.Last = Chr(160)
End If
End If
Wend
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
T

Tony Jollans

The point is that lines (and pages) are transient objects in Word and it is
often counter-productive trying to impose your own structure. The way to do
this is to add a non-breaking space after the word(s) - instead of a normal
space. Replace "on " - without the quotes, that's o,n, space - with "on^s" -
again without the quotes, o, n, caret, s. Word will then treat "on", the
non-breaking space, and the following character(s) as one word and not end
the line with on.
 
H

Helmut Weber

Hi Tony,

sure, that's the way.

I was thinking about answering the original question
instead of thinking about the problem.

:)

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

jlhamann

Thanks Tony, that was the information I needed.

Tony Jollans said:
The point is that lines (and pages) are transient objects in Word and it is
often counter-productive trying to impose your own structure. The way to do
this is to add a non-breaking space after the word(s) - instead of a normal
space. Replace "on " - without the quotes, that's o,n, space - with "on^s" -
again without the quotes, o, n, caret, s. Word will then treat "on", the
non-breaking space, and the following character(s) as one word and not end
the line with on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top