Word Application Object

G

Gabriel RIvera

I have a question regarding what happens when the user
hits the "X" (on the top right hand of the Word Window)
to close a Word application. Using

dim wapp as new word.application()

It seems pretty clear that if the user clicks on the X
then the statement (wapp is nothing) should be true and
this is not the case.

*CODE*****
If Not (wapp Is Nothing) Then

If wapp.Documents.Count > 0 Then
Dim intResponse As Integer

If myDoc.Saved <> True Then
...more code
****end code*************

Programs execution executes the code inside the if
statement above and it crashes on wapp.documents.count>0
The Following error is thrown "the RPC server is
unavailable".

NOTE: Please note that my program initially opens a Word
application and types stuff into it. I then manually
close the Word Application (as any curious client might)
and then i run my program again which runs the code
snippet above. This crashing is what i am trying to avoid
and i am kinda' stuck cause i don't know what the X does
in the first place and why is wapp not null when the x is
pressed. THanks for the help.


Gabriel
 
M

martinique

If you use:

dim wapp as new word.application()

then wapp will *never* evaluate as nothing. The whole point of the 'new'
keyword is that *any* reference to the object will cause it to instantiate,
if it isn't instantiated already.Never declare an object 'as new'.

Separately, if you create a reference to an object, then the object will
continue to exist as long as the reference exists (that's in the nature of
object references) -- objects continue to exist as long as there are
references to them. So the user clicking the application Close button will
not normally set your object reference to nothing. Word may disappear as far
as the user is concerned, but still exist as an object in memory.
 
G

Gabriel Rivera

The truth is that i have been using (after previous mail)

dim wapp as word.application()

and then

wapp= new word.application()

But even using this method, if the user closes the word
application the statement

if (wapp is nothing) then
end if

statement never executes.


Also forgetting about this question above, do i have to
manually dispose or set to nothing the word.application
object when i am done or is this done by VB when user
closes the form?

Sorry about all the questions, but given that VB does a
lot of stuff for you, i get stuck not knowing what i
should and should not do.

thanks again martinique,

gabriel
 
M

martinique

Yes, you must manually set the object to nothing. VBA will not do it for
you. (the object may be invalid, but it won't be nothing). If you're not
currently doing that, that's why your (wapp is nothing) doesn't work.
 
G

Gabriel Rivera

The problem is...when do i set the object to nothing?
I mean, I know when i want to set it to nothing but i am
not sure how to deal with the event handlers and so.

My program does basically this: It loads a form up and
then the user clicks on buttons,check boxes,drop downs
(data collection stuff) and eventually when he is done he
presses a button(button MAKEREPORT) Word opens and some
custom automated text prints out. THe user can then set
the data input forms to their default(empty ) values and
and start again.If he presses MAKEREPORT once again the
application checks if there is a documents open.(if so it
asks for the user to save and saves it if the user so
desires)
As you see i use the word application over and over again
and i would only like to discard the object when the user
completely exits my application (form). OR the other
problem is that once the user automates his/her word
documents he proceeds to close the word application and
go back again to my program. THis is when the error is
caused. Since wapp has been closed (at least in part..and
it is not null but the object has errors in it so i can't
use it or know when the user has closed the application)
RIght now i took care of this by executing

wapp=new word.application

in a try catch statement for the error that happens when
the user closes the word application. Though this is
cheap coding(which i would like to hear if you have any
new ideas), using event handlers such as
DocumentBeforeCLose,autoclose,docuement_close, proved
helpless.

So now, I guess I go back to the first question
again...So lets set the wapp to nothing when the user
wants to exit my form. How do i do this?Should the
garbage collector take care of wapp when my form closes?
How do i know when the user clicks on the X for my
program? And if i have to set the wapp object to nothing
why don't i have to set say all objects used to nothing
(or do I) say

Dim tmp as int32
tmp=8*9
does this mean i also have to set tmp to nothing and
every little bit of memory i used along the way?


Again thanks for the crash course Martinique.

-g
 

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