Microsoft KB Archive/895509

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

Article ID: 895509

Article Last Modified on 11/9/2006

-

APPLIES TO


 * Microsoft Office Project Professional 2007
 * Microsoft Office Project Professional 2003
 * Microsoft Project 2002 Professional Edition

-



INTRODUCTION
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 &quot;kernel32&quot; (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(, &quot;MSProject.Application&quot;)

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

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

'logon parameters for Project Server sServer = &quot;http://servername/projectserver&quot; ' for example, http://server85/projectserver sServerUser = &quot;Username&quot; ' 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 = &quot;Userpassword&quot; sServerString = &quot;winproj.exe /s &quot;&quot;&quot; & sServer & &quot;&quot;&quot; /u &quot;&quot;&quot; & sServerUser & &quot;&quot;&quot; /p &quot;&quot;&quot; & sServerPass & &quot;&quot;&quot;&quot; ' Replace projectname with the name of your project. sProjectName = &quot;projectname&quot;

'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 &quot;wait&quot; 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(, &quot;Msproject.Application&quot;)

On Error GoTo 0

p.FileOpen &quot;<>\&quot; & sProjectName p.WindowActivate sProjectName

Exit Sub

NotReady:

If Timer > t Then MsgBox &quot;Microsoft Project has not been able to start for 15 seconds. Try again later.&quot; Else Resume End If

End Sub

Additional query words: PRJ2007 project2003 prj2003 prj2k3 project2002 prj2002 prj2k2

Keywords: kbprogramming kbautomation kbvba kbhowto KB895509

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.