简体   繁体   中英

Check whether file is open or not-Excel VBA

I need to know that whether particular file is opened or not using VBA or VB.net. If it is opened, execute required actions. If file is not opened need to open the file and execute actions. EX.

If file is open then
     Do required things
else 
     **Open the file**
     Do required things
end if.

Found a by surfing INTERNET. But no answer.

Chandana

strong text emphasized text

I assume you mean opened by a particular instance of Excel. You will generally have a reference to an Excel.Application object, either implicitly (eg if your app is Excel VBA or VSTO VB.NET) or because you have explicitly created an instance.

You can then iterate through the Application.Workbooks collection, looking for an instance that matches the file you're interested in, for example, by comparing the Workbook.Path property.

Note that in Excel, you can't open two workbooks with the same filename (but different directories) simultaneously. So if you find a workbook with the same name as your file, but a different directory, you'll have to close it before opening your workbook.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM