473,504 Members | 13,621 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Word Locking Files

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!
Nov 16 '05 #1
3 3449
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]
- mv*@spam.guard.caspershouse.com

"John Smith" <js@no.com> wrote in message
news:uc**************@TK2MSFTNGP09.phx.gbl...
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!

Nov 16 '05 #2
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ef**************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"John Smith" <js@no.com> wrote in message
news:uc**************@TK2MSFTNGP09.phx.gbl...
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!


Nov 16 '05 #3
John,

If anything, I think that is probably the MOST reliable thing across all
versions =)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Smith" <js@no.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:ef**************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"John Smith" <js@no.com> wrote in message
news:uc**************@TK2MSFTNGP09.phx.gbl...
> 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!
>
>



Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
7298
by: Al Moritz | last post by:
Hi all, I was always told that the conversion of Word files to HTML as done by Word itself sucks - you get a lot of unnecessary code that can influence the design on web browsers other than...
1
615
by: ajk | last post by:
. Hi, All: I know how to insert files into a Word doc using C#. However, the program I've written to do this runs much too slowly. The "myObj".Application.Selection.InsertFile method executes...
3
4213
by: m3ckon | last post by:
Hi there, I can succesfully create a word doc from my asp.net page, but I have 2 issues I need to resolve in order to use it in my app: 1) Creating a table: I seem unable to create a table,...
15
6136
by: z. f. | last post by:
Hi, i have an ASP.NET project that is using a (Class Library Project) VB.NET DLL. for some reason after running some pages on the web server, and trying to compile the Class Library DLL, it...
0
876
by: alex | last post by:
I have few web pages that allow to upload, delete, etc images (.jpg, ..bmp, .gif) Once the page is doen working with file thru Bitmap class I close file handles as: Bitmap bmp = new...
5
3930
by: Adrian | last post by:
Is there a way to lock records and/or files in multi user application, using C#? Adrian.
15
5208
by: Matt Brandt | last post by:
I am trying to get multiple threads to lock specific regions of a file. However, since each thread has the same PID, it appears that a lock by one thread does not block another thread from the same...
5
2524
by: alivip | last post by:
How can I get every Token (word) and PreviousToken(Previous word) From multube files and frequency of each two word my code is trying to get all single word and double word (every Token (word) and...
3
2085
by: HugoVallejo | last post by:
Hi everyone. I've created an ASP.NET web site which uses a .NET assembly to read/write operations using an odbc driver. When a connection to the odbc database is made locking files are created, when...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.