Word Locking Files

J

John Smith

Hey folks,

I've got a Windows C# application that generates a .doc file and then allows
the user to preview it.

I generate the file using the StreamWriter class and do a flush() and a
close() after writing the last line (which always get runned).

Because the intended users have all versions of MS Word ranging from Word 97
to 2003, I chose not to go with a dll object modeling approach and instead
to embed a WebBrowser control and open the document in there. My code for
doing this comes from the MS example on MSDN.

Problem is, sometimes (randomly), after running the program the file that is
created remains locked by MS Word and cannot be overwritten and can only be
opened readonly. Task Manager shows WinWord still running. If I kill it in
TaskManager, the file is free again. Other times it works fine and none of
this happens. The data written to the file is always the same, so it's not
something going on there, and the lines of code executed are always the
same.

What do you think could be causing this to occur and how can I ensure that
the file is released and WinWord closes after the form with the WebBrowser
control closes?

Thanks!
 
N

Nicholas Paldino [.NET/C# MVP]

John,

When loading a document in a WebBrowser, the web browser eventually does
the loading of Word (in order to render). AFAIK, there is no way to get at
that application instance (at least, cleanly) in order to shut it down.

Chances are there is a reference to Word that the web browser control is
not releasing, causing it to linger (and keep the file locked).

As a workaround, when the form is closed, I would recommend navigating
the web browser to "about:blank" (to try and release the document), and then
(if need be), call Dispose, release the reference, and perform a GC. I
would do all of these things as needed, in order (if one step earlier solves
it, great).

Hope this helps.
 
J

John Smith

Thanks Nicholas. I agree that it is not closing the MS Word instance which
has the document locked, and I think I finally know why.

I'm currently debugging the application. Apparently (I was not aware of
this, but), if you stop the debug by clicking the Stop button rather than by
closing the forms of the application, the dispose methods aren't ever
called. I often do this. So, it seems that not all resources were
released, and in this case, Word never gets closed.

I imagine then, that if my application ever suddenly crashes on the client,
this will always remain a problem. Hopefully I'll have all the exceptions
caught.

What I'd really like to do is to just have it open the document in Word
itself. That would resolve some other issues for me as well. I wonder if
telling it to run the application winword.exe and passing it the file name
as a parameter is reliable across all versions of Word. Hmm....I'll have to
test that out.


Nicholas Paldino said:
John,

When loading a document in a WebBrowser, the web browser eventually does
the loading of Word (in order to render). AFAIK, there is no way to get at
that application instance (at least, cleanly) in order to shut it down.

Chances are there is a reference to Word that the web browser control is
not releasing, causing it to linger (and keep the file locked).

As a workaround, when the form is closed, I would recommend navigating
the web browser to "about:blank" (to try and release the document), and then
(if need be), call Dispose, release the reference, and perform a GC. I
would do all of these things as needed, in order (if one step earlier solves
it, great).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

John Smith said:
Hey folks,

I've got a Windows C# application that generates a .doc file and then
allows
the user to preview it.

I generate the file using the StreamWriter class and do a flush() and a
close() after writing the last line (which always get runned).

Because the intended users have all versions of MS Word ranging from Word
97
to 2003, I chose not to go with a dll object modeling approach and instead
to embed a WebBrowser control and open the document in there. My code for
doing this comes from the MS example on MSDN.

Problem is, sometimes (randomly), after running the program the file that
is
created remains locked by MS Word and cannot be overwritten and can only
be
opened readonly. Task Manager shows WinWord still running. If I kill it
in
TaskManager, the file is free again. Other times it works fine and none
of
this happens. The data written to the file is always the same, so it's
not
something going on there, and the lines of code executed are always the
same.

What do you think could be causing this to occur and how can I ensure that
the file is released and WinWord closes after the form with the WebBrowser
control closes?

Thanks!
 
N

Nicholas Paldino [.NET/C# MVP]

John,

If anything, I think that is probably the MOST reliable thing across all
versions =)


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

John Smith said:
Thanks Nicholas. I agree that it is not closing the MS Word instance
which
has the document locked, and I think I finally know why.

I'm currently debugging the application. Apparently (I was not aware of
this, but), if you stop the debug by clicking the Stop button rather than
by
closing the forms of the application, the dispose methods aren't ever
called. I often do this. So, it seems that not all resources were
released, and in this case, Word never gets closed.

I imagine then, that if my application ever suddenly crashes on the
client,
this will always remain a problem. Hopefully I'll have all the exceptions
caught.

What I'd really like to do is to just have it open the document in Word
itself. That would resolve some other issues for me as well. I wonder if
telling it to run the application winword.exe and passing it the file name
as a parameter is reliable across all versions of Word. Hmm....I'll have
to
test that out.


in
message news:[email protected]...
John,

When loading a document in a WebBrowser, the web browser eventually does
the loading of Word (in order to render). AFAIK, there is no way to get at
that application instance (at least, cleanly) in order to shut it down.

Chances are there is a reference to Word that the web browser control is
not releasing, causing it to linger (and keep the file locked).

As a workaround, when the form is closed, I would recommend
navigating
the web browser to "about:blank" (to try and release the document), and then
(if need be), call Dispose, release the reference, and perform a GC. I
would do all of these things as needed, in order (if one step earlier solves
it, great).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

John Smith said:
Hey folks,

I've got a Windows C# application that generates a .doc file and then
allows
the user to preview it.

I generate the file using the StreamWriter class and do a flush() and a
close() after writing the last line (which always get runned).

Because the intended users have all versions of MS Word ranging from Word
97
to 2003, I chose not to go with a dll object modeling approach and instead
to embed a WebBrowser control and open the document in there. My code for
doing this comes from the MS example on MSDN.

Problem is, sometimes (randomly), after running the program the file that
is
created remains locked by MS Word and cannot be overwritten and can
only
be
opened readonly. Task Manager shows WinWord still running. If I kill it
in
TaskManager, the file is free again. Other times it works fine and
none
of
this happens. The data written to the file is always the same, so it's
not
something going on there, and the lines of code executed are always the
same.

What do you think could be causing this to occur and how can I ensure that
the file is released and WinWord closes after the form with the WebBrowser
control closes?

Thanks!
 

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