使用GetObject检索项目文件后保持打开状态

时间:2010-09-30 19:36:07

标签: vba com ms-project

使用GetObject()打开MS Project文件的正确方法是什么?在另一个应用程序中宏结束后,用户可以看到这个文件是打开的?

我在网上找到的信息表明,在对象超出范围之前setting the Application.UserControl property为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

2 个答案:

答案 0 :(得分:1)

您可以创建应用程序的实例并在实例中打开项目文件,而不是使用GetObject吗?

Sub AddTasks()
   Dim msProj as Object
   Set msProj = CreateObject("Project.Application")

   msProj.FileOpen "C:\projtest.mpp"

   'do stuff to project file here

   msProj.Visible = True
End Sub

上面的内容(我无法测试上面的代码,因为我没有MSProject,但类似的代码适用于MSWord)

答案 1 :(得分:0)

对于Project UserControl,仅指示用户是否启动了应用程序;它似乎是只读的,因为它是。我没有完成你对Project的要求,虽然这是一个类似的例子,Word试图查看并找到运行的Excel实例。也许这有点帮助:

can-vba-reach-across-instances-of-excel