views:

52

answers:

2

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
A: 

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

ForEachLoop
+1  A: 

Instead of using GetObject, could you create an instance of the application and open the project file in the instance?

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

Something like the above (I can't test the above code because I don't have MSProject, but similar code works for MSWord)

Phydaux
"Project.Application" should read "MSProject.Application", and I have to use the ActiveProject property of the Application object since FileOpen doesn't return a reference, but this at least works. Thanks
Theran