使用 GetObject 检索项目文件后将其保持打开状态
在不同应用程序中的宏结束后,使使用 GetObject() 打开的 MS Project 文件保持打开状态并且对用户可见的正确方法是什么?
我在网上找到的信息表明,在对象退出之前将Application.UserControl属性设置为True范围应允许用户继续使用打开的文件。但是,至少对于 MS Project,Application.UserControl 属性似乎是只读的。有办法解决这个问题吗?
显示问题的简化示例:
Sub AddTasks()
Dim proj As Object
' Already have the file path from another part of the workflow
Set proj = GetObject("C:\projtest.mpp")
' perform some calculations and add new tasks to project
proj.Tasks.Add "additional task"
' Leave Project open and visible for the user
proj.Application.Visible = True
proj.Application.UserControl = True ' Gives "Type Mismatch" error
' without the UserControl line, runs ok, but Project closes after the end of the macro
End Sub
What is the right way to leave an MS Project file opened with GetObject() open and visible to the user after the end of a macro in a different app?
The information I found online suggests that setting the Application.UserControl property to True before the objects go out of scope should allow the user to continue using the opened file. However, for MS Project at least, the Application.UserControl property appears to be read-only. Is there a way to work around this?
A simplified example showing the problem:
Sub AddTasks()
Dim proj As Object
' Already have the file path from another part of the workflow
Set proj = GetObject("C:\projtest.mpp")
' perform some calculations and add new tasks to project
proj.Tasks.Add "additional task"
' Leave Project open and visible for the user
proj.Application.Visible = True
proj.Application.UserControl = True ' Gives "Type Mismatch" error
' without the UserControl line, runs ok, but Project closes after the end of the macro
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否可以创建应用程序的实例并在实例中打开项目文件,而不是使用 GetObject?
类似上面的东西(我无法测试上面的代码,因为我没有 MSProject,但类似的代码适用于 MSWord)
Instead of using GetObject, could you create an instance of the application and open the project file in the instance?
Something like the above (I can't test the above code because I don't have MSProject, but similar code works for MSWord)
对于Project UserControl,仅指示用户是否启动了应用程序;它看起来是只读的,因为它确实是只读的。我还没有完成您对 Project 的要求,尽管这里有一个类似的示例,用于 Word 尝试查看和查找正在运行的 Excel 实例。也许这有一点帮助:
can-vba-reach-across- excel 实例
For Project UserControl just indicates if the user started the application or not; it appears to be read-only because it is. I've not done what you're asking for with Project, although here is a similar example for Word trying to see and find running instances of Excel. Perhaps this helps a little:
can-vba-reach-across-instances-of-excel