WordAplication.Selection

Add-in Express™ Support Service
That's what is more important than anything else

WordAplication.Selection
 
Rafal Piotrowski




Posts: 12
Joined: 2005-08-26
Hi,
Is this pice of code correct

Dim Application as Word.Application 'Imports exists for Word
Application = myAddin.HostApplication

then in different places I make a calls to

Application.Selection.TypeText...
Application.Selection.TypeParagraph()
Application.Selection.MoveRigh/Left/Up/Down etc

in various order

My question is: Do I need to create a local variable
Dim sel as Word.Selection
then set sel to Application.Selection
and at the end release it?

I am asking because if I do this:
sel = Application.Selection
sel.TypeText...
sel.TypeParagraph() ' at this line I will get NullPointerException even if the sel object itself is not null

many thanks
rp
Posted 13 Sep, 2005 07:33:10 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Rafal.

Yes, I would advice you to use a local variable for the Selection object.
I have just tested the code below and all works fine:
Dim wApp As Word._Application
Dim Sel As Word.Selection

Try
wApp = HostApplication
Sel = wApp.Selection
Sel.TypeText("test")
Sel.TypeParagraph()
Catch err As Exception
MessageBox.Show(err.Message)
Finally
If Not (Sel Is Nothing) Then
Marshal.ReleaseComObject(Sel)
End If
End Try

What PIAs do you use in your project?
Posted 13 Sep, 2005 15:17:22 Top
Chris Cardinal


Guest


How do I get the Word Application instance from inside a SmartTag?
there is no Host property anywhere....
Posted 03 Dec, 2005 09:43:23 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Chris.

You can download an example by using the link below to learn how it can be done:

http://www.add-in-express.com/projects/addinandsmarttagexample.zip
Posted 03 Dec, 2005 10:40:48 Top
Chris Cardinal


Guest


Thanks..
Posted 05 Dec, 2005 08:06:33 Top