Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 895509 - Last Review: November 9, 2006 - Revision: 5.3
How to programmatically start Microsoft Project Professional from an Office program by using the Shell function in a VBA macro
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 "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
p.Quit
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
NotReady:
If Timer > t Then
MsgBox "Microsoft Project has not been able to start for 15 seconds. Try again later."
Else
Resume
End If
End Sub
APPLIES TO
- Microsoft Office Project Professional 2007
- Microsoft Office Project Professional 2003
- Microsoft Project 2002 Professional Edition
| kbprogramming kbautomation kbvba kbhowto KB895509 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please