1

There are lot of similar questions but not exactly what I am looking for.

I am using Excel Interop in C# for working with excel files. I want to specifically know whether Excel Interop's Workbooks.Open() method can detect if the file is already open. Are there any parameters that we can set so that the Workbooks.Open() method raises exception if file was already open by some other user. Currently, it doesn't seem to raise any exception if the file is in use by other user.

Please scope the answers to whether this is possible using Workbooks.Open(). There are already answers on stackoverflow related to using System.IO.File for this purpose.

CleanBold
  • 1,551
  • 1
  • 14
  • 37

2 Answers2

0

If the workbook is already open by the same excel process:

You can iterate through the Workbooks collection and check if the file is present in the collection. It should indicate that the file is already open.

bool isOpened = workbooks.FirstOrDefault(p=>p.Fullname == filename);

if (!isOpened){
  workbooks.Open(filename);
}

If the workbook is already open by some other process:

Workbooks.Open supports the following parameters

string Filename 
Object UpdateLinks,
Object ReadOnly,
Object Format,
Object Password,
Object WriteResPassword,
Object IgnoreReadOnlyRecommended,
Object Origin,
Object Delimiter,
Object Editable,
Object Notify,
Object Converter,
Object AddToMru,
Object Local,
Object CorruptLoad

The Open method does not provide any parameter for your behavior. Alternatively you could open the file in editable mode and check the property ReadOnly of the Workbook to see if the file is probably in use by another process.

var workbook = workbooks.Open(fileName,
                Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

if (workbook.ReadOnly){
  // is probably opened by another process;
}
Amyth
  • 32,527
  • 26
  • 93
  • 135
Jehof
  • 34,674
  • 10
  • 123
  • 155
  • Jehof - How about file being opened by other user in other excel process altogether. That is what I have asked for. – CleanBold Apr 28 '15 at 06:14
-1

You can use Try catch with workbooks.open() method to know whether a file is open.even though there are other specific methods to return if the file is open in another process,your idea is bit simple.try like this

try
{
workbooks.open(filename);
}
catch(exception e)
{
msgbox(e)
}

hope it helps.you can find more details here check if workbook is open

Community
  • 1
  • 1
akhil kumar
  • 1,598
  • 1
  • 13
  • 26
  • Workbooks.Open() is not raising exception if file is in use by other user. That is what I meant when I wrote: "Are there any parameters that we can set so that the Workbooks.Open() method raises exception if file was already open by some other user." Also I want the answered scoped to Workbooks.Open() method. Other alternative that you mentioned, I already found it on stackoverflow. – CleanBold Apr 28 '15 at 06:07