Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2005

Tip: Looking for answers? Try searching our database.

Word/Excel check for an existing file lock

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike Loux - 15 Sep 2005 15:52 GMT
Hi All,

Does anyone know of a way (in .NET) to determine if a Word or Excel
file is currently locked for editing by another user BEFORE trying to
open it?

I am using the DsoFramer control to embed Word/Excel documents into my
VB.NET app, and when a user tries to open a file that is locked by
another user, it causes Word/Excel to pop up a dialog to that effect.
In addition, it brings up an actual Word/Excel window as well, which
will definitely serve to confuse my users.

I would much rather be able to check the status of the file before
attempting to load it into Word/Excel, and then either open the file as
read-only (and notify the users via a status bar entry or a dialog), or
allow the user the same choices the Office dialog offers (Read Only,
Notify, Cancel).

Unfortunately, there does not seem to be a BeforeDocumentOpened event
in the DsoFramer control, so I will have to bring up an external
instance of Word/Excel to do the checking, but that's not a problem if
I can actually avoid opening the file before knowing what the status
is.  

Any ideas!  Thanks for your time, people!
Robert Jordan - 15 Sep 2005 23:21 GMT
Hi Mike,

> Does anyone know of a way (in .NET) to determine if a Word or Excel
> file is currently locked for editing by another user BEFORE trying to
> open it?

Try to open the file in read/write mode:

bool IsLocked (string fileName) {
  try {
    File.Open(fileName, FileMode.Open, FileAccess.ReadWrite).Close();
    return false;
  }
  catch (IOException) {
    return true;
  }
}

Rob
Mike Loux - 16 Sep 2005 14:16 GMT
Hi Rob,

You know, I never even thought of using standard file operations.   I
implemented your solution, and it DOES correctly identify the file as
being locked.  Yayyy!

Unfortunately, telling the DsoFramer control to open the file as
read-only didn't stop Word and Excel from popping up that "File has
been locked by so-and-so" dialog as I had hoped, so I'm back to square
one.

I know it is possible to tell Word/Excel (interactively through the
GUI) to open a file as read only off the bat without it popping up the
dialog, so there must be a communications problem between the DsoFramer
control and Word/Excel.

Good thing I still have an open ticket with Microsoft on this bad
boy...

Thanks for your help, Rob!  I will definitely keep the code you
supplied in my project.  Too bad MS chose to remove their support for
embedding office apps from .NET Framework 2.0.  Then I wouldn't have to
jump through hoops like this...

Slainte!
-Mike
Uday Takbhate [MSFT] - 19 Sep 2005 06:24 GMT
Michael,

This is a Word / Excel notification dialogue. It will appear whenever you
try to open a locked file. This is by design, even if you try to open a
locked file in a Word / Excel instance, which is not embedded in any
container, it will display the same notification. As DSOFramer is just
hosting the Office application, it can not suppress this dialogue or take
control of this dialogue. However, you can dispose this dialogue box by
sending keys / events to this dialog box.

Please refer to the following article for more information
How To Dismiss a Dialog Box Displayed by an Office Application with Visual
Basic
http://support.microsoft.com/kb/259971/EN-US/

Regards,
Uday Takbhate [MSFT]
Microsoft Developer Support
--------------------
>From: "Mike Loux" <mike.loux@gmail.com>
>Newsgroups: microsoft.public.dotnet.framework.interop
[quoted text clipped - 9 lines]
>Content-Type: text/plain; charset="iso-8859-1"
>X-Trace: posting.google.com 1126876582 18447 127.0.0.1 (16 Sep 2005
13:16:22 GMT)
>X-Complaints-To: groups-abuse@google.com
>NNTP-Posting-Date: Fri, 16 Sep 2005 13:16:22 +0000 (UTC)
>User-Agent: G2/0.2
>X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.8b4) Gecko/20050908 Firefox/1.4,gzip(gfe),gzip(gfe)
>Complaints-To: groups-abuse@google.com
>Injection-Info: g14g2000cwa.googlegroups.com; posting-host=24.97.250.4;
>   posting-account=CaSj6Q0AAABXqhUyMw8Kz7fvmDqCOYHz
>Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onli
ne.de!border2.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.gigan
ews.com!postnews.google.com!g14g2000cwa.googlegroups.com!not-for-mail
>Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.framework.interop:9667
>X-Tomcat-NG: microsoft.public.dotnet.framework.interop
[quoted text clipped - 25 lines]
>Slainte!
>-Mike
Mike Loux - 19 Sep 2005 14:43 GMT
Ahh, but if you specifically open the file as Read-Only in Word or
Excel, it will NOT display the dialog, even when the file has been
previously locked for editing by another user/process.

The DsoFramer control has a ReadOnly parameter in its Open method,
which does indeed open the file in Read Only mode (not locking it for
editing), but the dialog pops up anyway.  So I KNOW there's a way
around this.  

Slainte!
-Mike
Uday Takbhate [MSFT] - 20 Sep 2005 20:06 GMT
Mike,

Let me check with the DSOFramer source.
I'll let you know as soon as I get something!

Uday Takbhate [MSFT]
Microsoft Developer Support
--------------------
>From: "Mike Loux" <mike.loux@gmail.com>
>Newsgroups: microsoft.public.dotnet.framework.interop
[quoted text clipped - 10 lines]
>Content-Type: text/plain; charset="iso-8859-1"
>X-Trace: posting.google.com 1127137440 5059 127.0.0.1 (19 Sep 2005
13:44:00 GMT)
>X-Complaints-To: groups-abuse@google.com
>NNTP-Posting-Date: Mon, 19 Sep 2005 13:44:00 +0000 (UTC)
>In-Reply-To: <B2kKFqNvFHA.1364@TK2MSFTNGXA01.phx.gbl>
>User-Agent: G2/0.2
>X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.8b4) Gecko/20050908 Firefox/1.4,gzip(gfe),gzip(gfe)
>Complaints-To: groups-abuse@google.com
>Injection-Info: g44g2000cwa.googlegroups.com; posting-host=24.97.250.4;
>   posting-account=CaSj6Q0AAABXqhUyMw8Kz7fvmDqCOYHz
>Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onli
ne.de!news.glorb.com!postnews.google.com!g44g2000cwa.googlegroups.com!not-fo
r-mail
>Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.framework.interop:9703
>X-Tomcat-NG: microsoft.public.dotnet.framework.interop
[quoted text clipped - 10 lines]
>Slainte!
>-Mike
Mike Loux - 20 Sep 2005 20:53 GMT
Cool beans.  Thanks for your help!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.