Quiting Word.Application

J

jorgedec

I'm trying to use Office's FileSearch object to search for files.
I'm using Visual Studio 2002 C# and Word 2000.

The problem I am having is realeasing the Word.Application object.
When I try to use the Quit() method I get an error:


"Microsoft Word for Windows has encountered a problem and needs to
close. We are sorry for the inconvenience"


Either way I end up with multiple WinWord processes running.


Here is part of my code:


Office.FileSearch fs;
Word.Application w;
w = new Word.Application();
fs = w.FileSearch;


fs.NewSearch();


{do search - this part works}


System.Runtime.InteropServices.Marshal.ReleaseComObject(fs);
fs = null;
object saveChanges = false;
object missing = System.Reflection.Missing.Value;
w.Quit(ref saveChanges,ref missing,ref missing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(w);
w = null;


If I comment out all parts having to do with FileSearch (ie just create

Word.Application and then use Quit() )
it works and WinWord is terminated.


I think I need to get rid of the FileSearch object first, before using
Quit() but I am not sure how to do this.


jorge de cardenas
 
C

Cindy M.

Hi Jorgedec,

Try this order:
fs = null;
now quit Word
set the word object to null

At this point, what you should do, according to Andrew Whitechapel, is
force garbage collection, twice - IF you want to handle the release of
resources deterministically. (The .NET Framework would, eventually, get
around to cleaning up, but you can tell it do so.) He doesn't recommend
using ReleaseComObject because as soon as you do, you lose ALL pointers
to COM objects in the AppDomain. In any case, if you decide you want to
use ReleaseCOMObject, do this after having quit Word and set everything
to null.
I'm trying to use Office's FileSearch object to search for files.
I'm using Visual Studio 2002 C# and Word 2000.

The problem I am having is realeasing the Word.Application object.
When I try to use the Quit() method I get an error:


"Microsoft Word for Windows has encountered a problem and needs to
close. We are sorry for the inconvenience"


Either way I end up with multiple WinWord processes running.


Here is part of my code:


Office.FileSearch fs;
Word.Application w;
w = new Word.Application();
fs = w.FileSearch;


fs.NewSearch();


{do search - this part works}


System.Runtime.InteropServices.Marshal.ReleaseComObject(fs);
fs = null;
object saveChanges = false;
object missing = System.Reflection.Missing.Value;
w.Quit(ref saveChanges,ref missing,ref missing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(w);
w = null;


If I comment out all parts having to do with FileSearch (ie just create

Word.Application and then use Quit() )
it works and WinWord is terminated.


I think I need to get rid of the FileSearch object first, before using
Quit() but I am not sure how to do this.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
J

jorgedec

I am still getting the same error.
The error occurs when trying to execute Word.Application.Quit().

I found that if I do everything except Execute() the FileSearch,
WinWord quits just fine.
I can see it in the task manager being created and terminate.
 
C

Cindy M.

Hi Jorgedec,
I am still getting the same error.
The error occurs when trying to execute Word.Application.Quit().

I found that if I do everything except Execute() the FileSearch,
WinWord quits just fine.
I can see it in the task manager being created and terminate.
OK, I'd like to test this in VBA as well as in .NET. Could you
describe, briefly, what kind of search you're performing and what
you're doing with the result?

Note, please, that I can only test in .NET 2.0 (VS 2005), and
probably not in combination with Office 2000. It's possible we're
looking at a problem with the IAs .NET 1.0 has generated to work
with the Office 2000 library.

A developer who drops by in these groups from time to time created
and optimized a set of Office 2000 IAs and has made them available.
You could try downloading and using these in place of the ones your
system generated (assuming they'll be compatible with your version
of .NET)

http://www.thielen.com/users/dave/articles/Word2000_PIAs.zip

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17
2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
J

jorgedec

Cindy said:
Hi Jorgedec,


OK, I'd like to test this in VBA as well as in .NET. Could you
describe, briefly, what kind of search you're performing and what
you're doing with the result?

Note, please, that I can only test in .NET 2.0 (VS 2005), and
probably not in combination with Office 2000. It's possible we're
looking at a problem with the IAs .NET 1.0 has generated to work
with the Office 2000 library.

A developer who drops by in these groups from time to time created
and optimized a set of Office 2000 IAs and has made them available.
You could try downloading and using these in place of the ones your
system generated (assuming they'll be compatible with your version
of .NET)

http://www.thielen.com/users/dave/articles/Word2000_PIAs.zip

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17
2005)

To Cindy:

Thanks for your replies.

I'm using the Office.Application.FileSearch object because it lets me
search for files by name (like: a* - all files starting with a) and
more important search for files comtaining a certain string (like:
"programming project"). FileSearch can also be set to look only in a
given path or also in sub-directories.

I could probably program these functionalities myself, but I figure why
reinvent the wheel.

After doing the search [ FileSearch.Execute() ], FileSearch has a
property FoundFiles that I can iterate through and add to a ListBox.

I can create the Word.ApplicationClass and get FileSearch object from
Word.ApplicationClass. Start a new search. Set up all the properties
for the search.
As long as I don't Execute() the search I can Quit() the
Word.ApplicationClass.

If I Execute() the search then I get an error when I try to Quit() the
Word.ApplicationClass.

I think the key is how to get rid of the FileSearch object.

Jorge
 
C

Cindy M.

Hi Jorgedec,
I'm using the Office.Application.FileSearch object because it lets me
search for files by name (like: a* - all files starting with a) and
more important search for files comtaining a certain string (like:
"programming project"). FileSearch can also be set to look only in a
given path or also in sub-directories.
You didn't show us the code you're using to for FileSearch. I've been
researching a bit in some old KB archives and find there were a couple of
problems in Word 2000. Could you please show us the code that sets the
properties for FileSearch? More importantly, I need to know what kinds of
values you may pass to "Lookin".

I also find information that using wildcards can cause problems in older
versions of Office on Windows 2000 systems (you don't mention which
version of Windows is involved).

Given the extent of the instability of FileSearch prior to Office XP...
I could probably program these functionalities myself, but I figure why
reinvent the wheel.
...you should look into the file management features provided by the .NET
Framework. They are extensive, I'm not so sure you would need to do much
"reinventing". And they ought to run more quickly than going through the
COM automation interface.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
J

jorgedec

Hello Cindy,

Thanks for all your time, but I think I found the problem. By the way
I'm using WinXP pro.

Somewhere in my earlier searching I read something like 'that all
objects created by a COM object have to be released first before
releasing the COM obj.'
--------------------------------------
I was doing this before:
--------------------------------------

fs = w.FileSearch;
// do search here
for (int i = 0; i< n; i++)
{
this.listBox1.Items.Add(i.ToString() + " - " + fs.FoundFiles
);
}

then releasing fs(FileSearch object) and trying to close w ( my
Word.ApllicationClass object)

-----------------------------------------------------------------
I changed it to the following and it's working:
-----------------------------------------------------------------

fs = w.FileSearch
// do search here

Office.FoundFiles ff = fs.FoundFiles;

for (int i = 1; i<= n; i++)
{
this.listBox1.Items.Add(i.ToString() + " - " + ff);
}

// Now I release the FileSearch object and the FoundFiles object

while( System.Runtime.InteropServices.Marshal.ReleaseComObject(fs) !=
0)
{
System.Threading.Thread.Sleep(1000);
}
fs = null;

while( System.Runtime.InteropServices.Marshal.ReleaseComObject(ff) !=
0)
{
System.Threading.Thread.Sleep(1000);
}
ff = null;


object saveChanges = false;
object missing = Type.Missing; //System.Reflection.Missing.Value;

// Now .Quit() works without throwing an error

w.Quit(ref saveChanges,ref missing,ref missing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(w);
w = null;

The search part was always working it was getting rid of the
Word.Application object that was giving me problems. If I din't try to
Quit() I could do searches all day long but each time I was creating
another Word.Application. I would have lots of WinWord in my
TaskManager running.

By the way I not sure if the way I'm releasing the objects is exactly
correct (it works) but I saw different approaches in my searches. Any
tips would be appreciated.

Jorge
 

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