Excel VBA 打开同一个工作簿两次,但一个实例是只读的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8891057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:57:21  来源:igfitidea点击:

Excel VBA open same workbook twice, but one instance is read-only

excelvba

提问by Wes

I created a read-only shortcut to a workbook that is writeable. Doing this, I can simultaneously open separate read-only and writeable instances of the same workbook when I manually click the shortcut and the actual document separately. However I can't seem to duplicate this in VBA code. Trying to open the same workbook from the read-only instance using VBA code closes the read-only instance and leaves me with only the writeable one, EG

我为可写的工作簿创建了一个只读快捷方式。这样做,当我分别手动单击快捷方式和实际文档时,我可以同时打开同一工作簿的单独只读和可写实例。但是我似乎无法在 VBA 代码中复制它。尝试使用 VBA 代码从只读实例打开同一个工作簿会关闭只读实例,只剩下可写的一个,EG

Workbook.Open filename:=ActiveWorkbook.FullName, editable:=True

Running this from the read-only instance terminates the instance and replaces it with a writeable one. I'd like to be able to have both, but no idea how to do this? Reason is I want to be able to publish changes made in read-only instance to writeable one with user only needing write access to the writeable instance for a few seconds, enough time for the VBA code to open it, publish the changes, and quickly save and exit and re-activate read-only instance. This allows multiple people in a team to almost concurrently make changes to the same workbook without having to suffer the limitations of 'Share Workbook', while also simultaneously being able to view the document.

从只读实例运行它会终止该实例并将其替换为可写的实例。我希望能够同时拥有两者,但不知道如何做到这一点?原因是我希望能够将在只读实例中所做的更改发布到可写实例,用户只需要几秒钟对可写实例的写访问权限,有足够的时间让 VBA 代码打开它,发布更改,并快速保存退出并重新激活只读实例。这允许团队中的多人几乎同时对同一工作簿进行更改,而不必受到“共享工作簿”的限制,同时还能够查看文档。

But what if someone makes changes in between? Well, the user is told to reload the read-only instance if any changes have been made by someone else since the user opened the document, which just calls the above code where editable = false.

但是,如果有人在两者之间进行更改呢?好吧,如果自用户打开文档后其他人进行了任何更改,则告知用户重新加载只读实例,该文档仅调用上述代码,其中 editable = false。

Help greatly appreciated

非常感谢帮助

回答by Matt Donnan

This method will open the workbork in the current instance and therefore as you say will replace the current open workbork. You need to create a new excel application reference and open it using that e.g:

此方法将在当前实例中打开工作区,因此如您所说将替换当前打开的工作区。您需要创建一个新的 excel 应用程序引用并使用它打开它,例如:

Dim xlApp As New Excel.Application
Dim xlWrkBk As Excel.WorkBook

xlApp.WorkBooks.Open FILENAME, , READ ONLY (True/False)

Set xlWrkBk = xlApp.ActiveWorkbook

xlApp.Visible = True

Hopefully that should take you in the right direction : )

希望这应该带你走向正确的方向:)

回答by baldmosher

Open the workbook Editable, then SaveCopyAs to a temp folder with a temp filename (must be different filename), then open the copy Read-Only

打开工作簿 Editable,然后 SaveCopyAs 到具有临时文件名的临时文件夹(必须是不同的文件名),然后打开副本只读