Finding the previous line

J

James

I don't know if line is the proper terminology, but what I need to do
is move up from a table to the first line of text which comes before
the table. Any ideas?
 
H

Helmut Weber

Hi James,
like this:
With Selection
.Collapse
.ExtendMode = False
.Tables(1).Range.Previous.Select
.HomeKey unit:=wdLine
End With
You'll get an error, if the start of the selection, is not in a table.
And another error, if the table is right at the start of the document.
And there may be more complications.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

James

I'm getting err#91, object var or with block var not set. I'm using the
exact code you put up. The selection is in the table and the table is
not at the start of the document. Any ideas?
 
H

Helmut Weber

Hi James,
hmm... no.
Usually this is trivial. Really no typos?
I would try it step by step, like
With selection
.Collapse
end with
With selection
.ExtendMode = False
end with
etc...
Can't believe it, but You never know...
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

James

..Tables(1).Range.Previous.Select <- that is the offending line. I don't
understand, because in earlier code I reference
activedocument.tables(1) with no problem.??
 
J

Jeff

This may not be the ultimate in elegance but it works. I think by "line" you
mean "paragraph". Individual lines within paragraphs are distributed by
page formatting and I don't think they can be selected as-such.

This sets the cursor to the start of the previous paragraph if the cursor is
within a table and the table is not the first object in the document.

Sub locate_before_table()
Dim TableRange, PrevLine As Range

If Selection.Information(wdWithInTable) Then
Set TableRange = Selection.Tables(1).Range
If TableRange.Start > 0 Then
Set PrevLine = ActiveDocument.Range(Start:=TableRange.Start - 1,
End:=TableRange.Start - 1)
Set PrevLine = PrevLine.Paragraphs(1).Range
PrevLine.Select
Selection.Collapse
End If
End If

End Sub
 
J

James

Even though I've selected the whole table, the line:
If TableRange.Start > 0 Then still equals zero. How can this be?
 
J

James

TableRange.Start always equals 0 if I run the above code and the table
is selected. How does the start property work? Why would it return 0?
 
J

James

I keep getting tablerange.start=0. How does the start property work?
When would it return 0?
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < James > écrivait :
In this message said:
TableRange.Start always equals 0 if I run the above code and the table
is selected. How does the start property work? Why would it return 0?

Is the table the first thing on the first page of the document?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
H

Helmut Weber

Hi James,
you have to show us more of your code.
BTW, I used selection.tables, not activedocument.tables,
got the first table in the selection, collapsed the selection,
just in case it isn't, set extendmode to false, just in case again,
select the character that preceeds the table's range and then move
the selection to the start of the line this character is on.
I don't see, what could be wrong with
.Tables(1).Range.Previous.Select
Works with activedocument and works with range.
But .HomeKey unit:=wdLine would not work in code like
with activedocument
.homekey ...
end with
Only with selection.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

Jeff

Goto the top of the document (ctrl-Home) and hit a few "enter" keys
This will put a few lines in front of your table

Then try again
 
J

James

What I have in the document is one word in a paragraph followed by a
table. If I select the whole table it appears that tablerange.start
includes the word in its count. Why would it do this?
 
H

Helmut Weber

Hi Jeff,
are you really sure?
If a doc starts with a table, you have to split the table,
because the very start (Ctrl-Home) of the doc is in the table.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

Jeff

Try it

Create a new doc and insert a table at the beginning
Then press ctrl-home, enter

Voila!

As you're an MVP you made me wonder if it was platform specific but I have
just tested this on W97 W2000 WXP and W2003
PS I'm an MS Word developer <:))
 
J

Jeff

You must be mistaken! You must also have some text selected AFTER the table
Simply use a more robust test...
"If Selection.Tables.Count > 0 Then"

This is the new code...


Sub locate_before_table()
Dim TableRange, PrevLine As Range

If Selection.Tables.Count > 0 Then
Set TableRange = Selection.Tables(1).Range
If TableRange.Start > 0 Then
Set PrevLine = ActiveDocument.Range(Start:=TableRange.Start - 1,
End:=TableRange.Start - 1)
Set PrevLine = PrevLine.Paragraphs(1).Range
PrevLine.Select
Selection.Collapse
End If
End If

End Sub
 
J

James

so getting back to my question...why tablerange.start include not only
the table selected but also the word in the previous paragraph?
 
J

James

No, because if I fiddle with the length of the word preceding the
table, the tablerange.start result changes as a result.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Jeff > écrivait :
In this message said:
This may not be the ultimate in elegance but it works. I think by "line" you
mean "paragraph". Individual lines within paragraphs are distributed by
page formatting and I don't think they can be selected as-such.

Yes they can, but you are right in that there is no "Line" collection in the
Word Object model while there are Story, Section, Paragraph, Word and
Character collections.

Try:
Selection.Bookmarks("\Line").Range.Select

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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