如何使用 addOLEObject 函数以只读方式打开文件?

How to use addOLEObject function to open a file for Read-Only?

我在 PPT 幻灯片中有一个按钮 [DisplayChart],它使用 AddOLEObject 将 Excel 图表嵌入到幻灯片中。

Set shapeOnPPT = 
ActivePresentation.Slides.FindBySlideID(mySlideID).Shapes.AddOLEObject(Left:=100,_
Top:=100, Width:=500, Height:=400, FileName:="c:\ThisDoc\Tester123.xlsx", Link:=msoTrue)

问题

当我进入演示者视图并单击 [DisplayButton] 并且 Tester123.xlsx 被另一个用户打开时,PPT 将 'hang'。按 ctrl-alt-del 显示 Excel 应用已打开,并显示以下提示消息:

单击只读或通知将关闭 Excel 应用程序,PPT 将不再挂起。

我试过的

我认为我需要自动以只读方式打开 testing.xlsx。根据 Microsoft's documentation,我似乎可以使用 Link参数。

Determines whether the OLE object will be linked to the file from which it was created. If you specified a value for ClassName, this argument must be msoFalse.

但是,将此参数更改为msoTruemsoFalse仍然没有解决任何问题。

P.s。在Windows7中,不会出现这个问题。我目前正在使用 Windows 10.

无法测试,但您可以试试这个

Application.DisplayAlerts = false   
Set shapeOnPPT = 
ActivePresentation.Slides.FindBySlideID(mySlideID).Shapes.AddOLEObject(Left:=100,_
Top:=100, Width:=500, Height:=400, FileName:="c:\ThisDoc\Tester123.xlsx", Link:=msoTrue)    
Application.DisplayAlerts = true

显然,没有 parameter/function 允许我们在添加 OLE 对象时执行此操作。最接近的方法是在打开文件之前使用 SetAttr function 将文件设置为只读。这允许多个程序查看文件的内容。

例如

SetAttr "c:\ThisDoc\Tester123.xlsx", vbReadOnly
Set shapeOnPPT = 
ActivePresentation.Slides.FindBySlideID(mySlideID).Shapes.AddOLEObject(Left:=100,_
Top:=100, Width:=500, Height:=400, FileName:="c:\ThisDoc\Tester123.xlsx", Link:=msoTrue)
SetAttr "c:\ThisDoc\Tester123.xlsx", vbNormal

注意:请记住在拥有 运行 功能后将其设置为 vbNormal 否则当其他程序尝试访问该文件时可能会出现问题。

此外,如果您的应用程序在 .addOLEObject 正在 运行 期间崩溃,那么一个重大风险是,该文件仍将处于只读状态,您将不得不手动将其改回。