[英]Opening a workbook (or other Office doc) on a shared drive that another user has open
Suppose a workbook exists on a shared network drive, and is open by User A. If User B attempts to open this file by double clicking it, he gets something like the following prompt:假设一个工作簿存在于共享网络驱动器上,并且由用户 A 打开。如果用户 B 尝试通过双击打开该文件,他会得到类似以下提示:
If User B instead attempts to open the workbook programmatically (perhaps with an add-in) using:如果用户 B 尝试使用以下方法以编程方式(可能使用加载项)打开工作簿:
Workbooks.Open("N:\path_to_workbook\workbook.xlsx")
what happens, exactly?究竟会发生什么? Does this above prompt appear?出现上面这个提示吗? If so, can it be suppressed with Application.DisplayAlerts = False
?如果是这样,是否可以使用Application.DisplayAlerts = False
来抑制它? If Workbooks.Open
as shown above throws an exception in this case, do we need to try again using the ReadOnly argument, as:如果Workbooks.Open
在这种情况下如上所示抛出异常,我们是否需要使用 ReadOnly 参数再次尝试,如:
Workbooks.Open("N:\path_to_workbook\workbook.xlsx", ReadOnly:=True)
I have read other posts (including this one ) that suggest that Workbooks.Open
in the first code snippet succeeds and the workbook is opened read-only, although this is not perfectly clear.我已经阅读了其他帖子(包括这个),这些帖子表明第一个代码片段中的Workbooks.Open
成功并且工作簿以只读方式打开,尽管这并不完全清楚。
Unfortunately, I don't have a test environment configured to replicate this scenario myself, which would otherwise be the obvious answer.不幸的是,我没有配置测试环境来自己复制这个场景,否则这将是显而易见的答案。
I tested on our network drive environment, and found the following.我在我们的网络驱动环境上进行了测试,发现如下。 If the file is opened by User A and User B attempts to open the file programmatically using Workbooks.Open
then the result will be a Read Only version of the file.如果文件由用户 A 打开,而用户 B 尝试使用Workbooks.Open
以编程方式打开文件,则结果将是文件的只读版本。
If you wish to control the opening of Read Only files than you may utilize the following code example to close it:如果您希望控制只读文件的打开,则可以使用以下代码示例将其关闭:
Sub Test()
Dim wb As Workbook
Set wb = Workbooks.Open("Path_To_Excel_File")
If wb.ReadOnly Then
MsgBox "File already in use"
wb.Close savechanges:=False
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.