【问题标题】:Leaving a Project file open after retrieving it with GetObject使用 GetObject 检索项目文件后保持打开状态
【发布时间】:2010-10-06 20:36:16
【问题描述】:

在其他应用程序中的宏结束后,让使用 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

【问题讨论】:

    标签: vba com ms-project


    【解决方案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)

    【讨论】:

    • “Project.Application”应该是“MSProject.Application”,我必须使用 Application 对象的 ActiveProject 属性,因为 FileOpen 不返回引用,但这至少有效。谢谢
    【解决方案2】:

    对于 Project UserControl 仅指示用户是否启动了应用程序;它似乎是只读的,因为它是。我没有完成您对 Project 的要求,尽管这是 Word 尝试查看和查找正在运行的 Excel 实例的类似示例。也许这有点帮助:

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

    【讨论】:

      猜你喜欢
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 2015-07-23
      • 2011-04-24
      相关资源
      最近更新 更多