Shauna
Wed Jun 27 05:31:59 CDT 2007
Hi Irr
I don't speak C# very well, but in VB(A), the code would be as follows.
1. Move to last line on the current page
Word has only the vaguest idea of what a "page" is. See
http://daiya.mvps.org/wordpages.htm for more info.
Word does have a pre-defined bookmark ("\Page") which is the page at the
current selection point. However, it has its problems. The end of the \Page
bookmark's range is actually the top of the next page ... except on the last
page of a document where the \Page range does not include the last paragraph
mark. So having got the range of the \Page, you have to bring it back one
character to get to the end of the page you're actually interested in.
Further, the \Page range returns the wrong range if a table straddles a
page.
(Isn't this fun?!)
So assuming that docMyDocument is your document, the following will work on
all but the last page of a document, unless a table goes across more than
one page:
Dim oRange As Word.Range
Set oRange = docMyDocument .Bookmarks("\Page").Range
With oRange
.Collapse Direction:=Word.WdCollapseDirection.wdCollapseEnd
.MoveEnd Unit:=Word.WdUnits.wdCharacter, Count:=-1
.Select
End With
Note: In recent versions, Word has exposed a Pages collection. But it is a
property of the Window Pane. And I can't see any way to get to that from the
Selection, which is the object you need to tell you where the insertion
point is.
2. Move to the last line of the document
There are lots of different ways to do this. Here's one:
Assuming that docMyDocument is your document, then
docMyDocument .StoryRanges.Item(wdMainTextStory).Select
Word.Application.Selection.HomeKey Unit:=wdStory, Extend:=wdMove
3. Searching for text
Dim oRange As Word.Range
Set oRange = docMyDocument .Range
With oRange.Find
.ClearFormatting
.Text = "Your text to find"
If .Execute Then
'your text was found
'and oRange will now
'be at the found text
'and .Found will return True
End If
End With
4. "add another line under it"
You'll have to tell us what you have in mind here. Do you mean to underline
the found text? Or to add text after the found text? For what it's worth,
Word has only a hazy idea of what a "line" is. Its fundamental unit of
understanding is the paragraph. Word creates a "lines" on the fly depending
on what fits in the available space. Let us know what you need to do to the
found text.
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
"Irr" <Irr@discussions.microsoft.com> wrote in message
news:CE93822E-9FCD-49A9-ACEC-701F4AB8D65A@microsoft.com...
> Since I can't edit my own post, I would also like to find out how to
> search
> for a certain text and add another line under it hehe.
>
> "Irr" wrote:
>
>> Hallo,
>>
>> I'm fumbling around with C#, Interop and Word.
>> I'm wondering how I could set focus on the page so that the cursor is on
>> the
>> line just above the footer, or the last line on the MainDocument?