How to programmatically start Microsoft Project Professional from an Office program by using the Shell function in a VBA macro


This article describes how to programmatically start Microsoft Project Professional from a Microsoft Office program by using the Shell function in a Microsoft Visual Basic for Applications (VBA) macro.

More Information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

To programmatically start Microsoft Project Professional from an Office program by using the Shell function in a VBA macro, create a VBA macro that is similar to the following macro example.
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub StartMSProject()
Dim sServer As String
Dim sServerUser As String
Dim sServerPass As String
Dim sServerString As String
Dim sProjectName As String
Dim t As Long
Dim p As Object
Dim b As Boolean

On Error Resume Next

Set p = GetObject(, "MSProject.Application")

' Determine whether Project is already running.
If Not p Is Nothing Then
b = True
MsgBox "Microsoft Project is already running. It will now be shut down. Please restart the macro."
End If

'If Project is running, shut down Project. Then, you must restart this macro.
If b Then
Exit Sub
End If

'logon parameters for Project Server
sServer = "http://servername/projectserver" ' for example, http://server85/projectserver
sServerUser = "Username"
' If the sServerPass password is blank, the user must click OK
' when the server logon dialog box appears. Note that we do not recommend a blank password. A non-blank password will log the user on automatically without
' user interaction.
sServerPass = "Userpassword"
sServerString = "winproj.exe /s """ & sServer & """ /u """ & sServerUser & """ /p """ & sServerPass & """"
' Replace projectname with the name of your project.
sProjectName = "projectname"

'Start Project. If you want to start with a Windows logon, remove the /u and /p parameters
'from the ServerString
Shell sServerString

'At this point, fail to an error handler if the application object cannot be created.
'This behavior occurs because the Shell function does not "wait" for the application to start; the code continues
'to execute. Because Project may be busy logging on, the following Set statement may fail. This
'artificial 'loop' gives Project time to finish the logon process.
t = Timer + 15 ' get the current time + 15 seconds
Sleep 15000 'Wait 15 seconds to give Project to finish the logon process.

On Error GoTo NotReady

'get object to project for automation
Set p = GetObject(, "Msproject.Application")

On Error GoTo 0

p.FileOpen "<>\" & sProjectName
p.WindowActivate sProjectName

Exit Sub


If Timer > t Then
MsgBox "Microsoft Project has not been able to start for 15 seconds. Try again later."
End If

End Sub

ID do Artigo: 895509 - Última Revisão: 18 de set de 2011 - Revisão: 1