简体   繁体   English

检查文件是否打开-Excel VBA

[英]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. 我需要知道是否使用VBA或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. 通过浏览INTERNET找到一个。 But no answer. 但是没有答案。

Chandana 昌达纳

strong text emphasized text 强文本 强调文本

I assume you mean opened by a particular instance of Excel. 我假设您的意思是通过特定的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. 通常,您将隐式地(例如,如果您的应用程序是Excel VBA或VSTO VB.NET)或由于您已经显式创建了一个对Excel.Application对象的引用。

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. 然后,您可以遍历Application.Workbooks集合,以查找与您感兴趣的文件匹配的实例,例如,通过比较Workbook.Path属性。

Note that in Excel, you can't open two workbooks with the same filename (but different directories) simultaneously. 请注意,在Excel中,不能同时打开两个具有相同文件名(但目录不同)的工作簿。 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. 因此,如果找到与文件名称相同但目录不同的工作簿,则必须在打开工作簿之前将其关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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