Connect to a project server when opening MS Projec

2020-02-07 06:33发布

问题:

I have a vbscript that checks to see if MS Project is open. If it's already open it runs a macro if not it should open Project then run the macro. It works fine if Project is already open. If project isn't open the script successfully opens and runs the macro but fails half way through. Basically it fails because the macro that is being called opens files from project server. even with my default account set to the Project server url and 'when starting' set to 'choose my default account' it still fails.

vbscript to open & run macro:

dim pjApp

on error resume next
set pjApp = GetObject(, "MSProject.Application")

    if err.Number = 0 then
    pjApp.Visible = True    
    pjApp.macro "testsave"
    else 
    Set pjApp = CreateObject("MSProject.Application") 
    pjApp.Visible = True 
    pjApp.macro "testsave"
    end if
Set pjApp = Nothing 

Is there a way of forcing it to connect to the project server site when Project opens?

回答1:

Here is the real issue:

Basically it fails because the macro that is being called opens files from project server.

In order to automate MS Project and have it open to a project server, you need to launch winproj.exe using a command-line switch as follows:

VBScript

On Error Resume Next
Dim pjApp 
Set pjApp = GetObject(, "MSProject.Application")
If Err.Number <> 0 Then
    Dim ProjServer
    ProjServer = Chr(34) & "enter project server name here" & Chr(34)
    Set objShell = WScript.CreateObject("WScript.Shell")
    objShell.Run "winproj /s " & ProjServer, 1, True
    Set objShell = Nothing 
    WScript.Sleep 5000
    Set pjApp = GetObject(, "MSProject.Application")
End If
pjApp.Macro "testsave"

The code first checks to see if MS Project is already open and if so, uses that instance. Otherwise it uses the shell command to open to a specific project server.

Note: Update sleep value as necessary to give MS Project enough time to open before trying to get a reference to it.

VBA version

On Error Resume Next
Dim pjApp As MSProject.Application
Set pjApp = GetObject(, "MSProject.Application")
If Err.Number <> 0 Then
    Dim ProjServer As String
    ProjServer = Chr$(34) & "enter project server name here" & Chr$(34)
    Shell "C:\Program Files (x86)\Microsoft Office\Office14\Winproj.exe /s " & ProjServer, vbNormalFocus
    Do While pjApp Is Nothing
        DoEvents
        Set pjApp = GetObject(, "MSProject.Application")
    Loop
End If
pjApp.Macro "testsave"

Note: Update the path to Winproj.exe as necessary.

Documentation for command-line switches seems to have been removed from Microsoft's site. Here's a page that still provides the documentation: Using Command-line switches for Project. Briefly:

  • /s "URL"
  • /u "username"
  • /p "password"
  • filename
  • -ProjectProfiles


回答2:

In place of pjApp.macro try:

pjApp.Appllication.Run "testsave"

I know that it is weird that the .macro version works for the code above it; however, it is still worth a shot. This code should also work for both places where you are using the .macro method.