Selection object - Word Automation

M

Mindy

I want to write some code to go to a specific place in a
long WOrd document. The document continues to grow as the
code runs, so "m_intCurrentNumPages" is the current number
of pages. I use "m_intCurrentNumPages - 1" so that the
range will be the next to last page (ie, I just want to
look at the last two pages of the document)

Dim oRange As Word.Range
Dim oSelection As Word.Selection

oSelection.GoTo(What:=Word.WdGoToItem.wdGoToPage, _
Which:=(m_intCurrentNumPages - 1))

oRange.Start = oSelection.Start
oRange.End = oDoc.Content.End
oRange.Select()

On the oSelection.GoTo line, an error occurs: "Object
reference not set to an instance of an object."

I cannot do "Dim oSelection As NEW Word.Selection"... The
New keyword creates a compile error ("new cannot be used
on an interface"), so I am not sure where my object
reference is not set.

On this line of code, I am trying to tell it to go to a
specific page. Do I have the "Which" value set wrong? Does
it have to be a wd Word constant? If so, how do I
specify, "go to page 14"

I am VB.NET and am automating a Word document object.
 
J

Jay Freedman

Hi, Mindy,

I assume you have a Word object, say oWordApp, that refers to the open
instance of Word. Instead of oSelection, use oWordApp.Selection to make .Net
stop complaining. Or, if you really like oSelection, then set oSelection =
oWordApp.Selection before you use it for anything.

You don't say anything about how you're getting a value for
m_intCurrentNumPages. The easy way is
oDoc.Repaginate
m_intCurrentNumPages = _
oWordApp.Selection.Information( _
Word.WdInformation.wdNumberOfPagesInDocument)
 
M

Mindy

Thank you for your reply. I have one more question. Here
is what I did...

Dim oRange As Word.Range
Dim oSelection As Word.Selection

'added this to associate the oSelection obj with its
'parent, the Word app, as you suggested...
oSelection = oDoc.Application.Selection

'... and this line now runs fine!!
oSelection.GoTo(What:=Word.WdGoToItem.wdGoToPage, _
Which:=Word.WdGoToDirection.wdGoToAbsolute, _
Count:=CType(m_intCurrentNumPages, Integer))

'now that i've moved past that error, now VB is throwing
'an exception on the oRange object
'(i thought I could set oDoc.Application.Range
'like i did for oSelection but .Range is not a prop of
'the Application object
oRange.Start = oSelection.Start
oRange.End = oDoc.Content.End
oRange.Select()

Ideas? Appreciate your input very much.
-----Original Message-----
Hi, Mindy,

I assume you have a Word object, say oWordApp, that refers to the open
instance of Word. Instead of oSelection, use
oWordApp.Selection to make .Net
 
J

Jay Freedman

Hi, Mindy,

If you're going to try to automate Word from VB.Net, you're going to
have to try to understand some of Word's object model. You can find a
diagram of it in the Word VBA Help file in the topic "Word Object
Model".

The Application object is at the top of the heap. The Documents
collection (and each document in it) is a member of the Application,
and the Selection is a property of the Application. Both the Document
object and the Selection have .Range properties.

Another (somewhat obscure) point is that when you declare a Range
object, you have to assign some preexisting range (such as oDoc.Range
or oSelection.Range) to it before you can set its Start and End
properties. That's why you're getting the error you mentioned.

Now that I'm at home where I have access to VB.Net, I've put together
a demo that actually works. Try single-stepping through this in the
debugger to understand what it's doing.

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim oWordApp As Word.Application
Dim oDoc As Word.Document
Dim oRange As Word.Range
Dim oSelection As Word.Selection
Dim m_intCurrentNumPages As Integer

oWordApp = CreateObject("Word.Application")
If oWordApp Is Nothing Then
MsgBox("CreateObject failed")
Exit Sub
End If
oWordApp.Visible = True

oDoc = oWordApp.Documents.Open(FileName:= _
"c:\Documents and Settings\Jay\" & _
"My Documents\Lorem.doc")
' change to a file that exists on your PC

oSelection = oWordApp.Selection
' *not* oDoc.Application.Selection;
' although that works, it's sloppy
oDoc.Repaginate()
m_intCurrentNumPages = oSelection.Information( _
Word.WdInformation.wdNumberOfPagesInDocument)

oSelection.GoTo(What:=Word.WdGoToItem.wdGoToPage, _
Which:=Word.WdGoToDirection.wdGoToAbsolute, _
Count:=m_intCurrentNumPages - 1)
' you said you wanted the last *two* pages

oRange = oSelection.Range
'oRange.Start = oSelection.Start is now not necessary
oRange.End = oDoc.Content.End
oRange.Select()
End Sub
 
M

Mindy

Thank you very much for your detailed explanation. This
helps a lot. Appreciate your time!!
-----Original Message-----
Hi, Mindy,

If you're going to try to automate Word from VB.Net, you're going to
have to try to understand some of Word's object model. You can find a
diagram of it in the Word VBA Help file in the topic "Word Object
Model".

The Application object is at the top of the heap. The Documents
collection (and each document in it) is a member of the Application,
and the Selection is a property of the Application. Both the Document
object and the Selection have .Range properties.

Another (somewhat obscure) point is that when you declare a Range
object, you have to assign some preexisting range (such as oDoc.Range
or oSelection.Range) to it before you can set its Start and End
properties. That's why you're getting the error you mentioned.

Now that I'm at home where I have access to VB.Net, I've put together
a demo that actually works. Try single-stepping through this in the
debugger to understand what it's doing.

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim oWordApp As Word.Application
Dim oDoc As Word.Document
Dim oRange As Word.Range
Dim oSelection As Word.Selection
Dim m_intCurrentNumPages As Integer

oWordApp = CreateObject("Word.Application")
If oWordApp Is Nothing Then
MsgBox("CreateObject failed")
Exit Sub
End If
oWordApp.Visible = True

oDoc = oWordApp.Documents.Open(FileName:= _
"c:\Documents and Settings\Jay\" & _
"My Documents\Lorem.doc")
' change to a file that exists on your PC

oSelection = oWordApp.Selection
' *not* oDoc.Application.Selection;
' although that works, it's sloppy
oDoc.Repaginate()
m_intCurrentNumPages = oSelection.Information( _
Word.WdInformation.wdNumberOfPagesInDocument)
_
Which:=Word.WdGoToDirection.wdGoToAbsolute, _
 

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