Saturday, January 19, 2013

How to check an Excel file is already open

There are no direct methods using Exel/Workbook object to find the functionality.

Work around is, we need to find out all open tasks, and from that we will see whether any task with name "Microsoft Excel"

Here in the below code, i have a sample Excel file with name "SampleXL".

Set Word = CreateObject("Word.Application")
Set Tasks = Word.Tasks
i=0
For Each Task in Tasks
    If instr(Task.Name,"Microsoft Excel - SampleXL")>0  Then
            i=1
    end if
Next
If i=1 Then
    print "Excel file is opened"
else
    print "No excel file opened with the name specified"
End If

Word.Quit

1 comment:

Avinandan said...

Hi,

I found your code is very helpful to me. I'm able to detect that my target excel file is opened or not.

Now I want to close the file if it found opened. Could you please help me to do that?

Thanks and regards,

Avinandan Chakrabarti