Vous êtes sur la page 1sur 8

VBAProgrammingfor

MicrosoftProject
98through2010
withanIntroductiontoVSTO

Rod Gill
Microsoft Project MVP

VBAProgrammingforMicrosoftProject
98through2010withanIntroductiontoVSTO
Copyright2011RodGill
Publisher:
Author:
Tech Editor:
Copy Editor:
Cover Design:

Chefetz LLC
Rod Gill
Jack Dahlgren
Rodney L. Walker
Emily Baker

ISBN:

978-1-934240-21-2

LCCN:

2011920851

Allrightsreserved.Youmaynotreproduceortransmitanypartofthisworkinanyformorby
anymeans,electronicormechanical,includingphotocopying,recording,orbyanyinformation
storage or retrieval system, without the prior written consent of the copyright owner and the
publisher.
We use trademarked names in this book in an editorial context and to the benefit of the trade
markownerwithnointentionofinfringementofthetrademark.
PublishedanddistributedbyChefetzLLCdba MSProjectExperts,90JohnStreet,Suite404,New
York,NY10038.(646)7361688http://www.msprojectexperts.com
We provide the information contained in this book on an as is basis, without warranty. Alt
houghwemakeeveryefforttoensuretheaccuracyofinformationprovidedherein,neitherthe
authorsnorthepublishershallhaveanyliabilitytoanypersonorentitywithrespecttoanyloss
ordamagecausedorallegedlycauseddirectlyorindirectlybytheinformationcontainedinthis
work.
MSProjectExperts publishes a complete series of rolebased training/reference manuals for Mi
crosoftsEnterpriseProjectManagementsoftwareincludingMicrosoftProjectandMicrosoftPro
ject Server. Use our books for selfstudy or for classroom learning delivered by professional
trainersandcorporatetrainingprograms.Tolearnmoreaboutourbooksandcoursewareseries
for Administrators, Implementers, Project Managers, Portfolio Managers, Resource Managers,
Executives, Team Members, and Developers, or to obtain instructor companion products and
materials, contact MSProjectExperts by phone (646) 7361688 or by email
info@msprojectexperts.com.

ii

Contents
AbouttheAuthor....................................................................................................................................................ix
DownloadtheSampleFiles..................................................................................................................................ix
AuthorsIntroduction..............................................................................................................................................x

SECTION1:MicrosoftProjectVBAEssentials.....................................................................1
IntroducingProjectVBA.........................................................................................................................................3
AddingtheDeveloperTabtotheProjectRibbon..............................................................................................3
SampleProjectFiles.................................................................................................................................................4
Module01:DefiningandUsingCustomFields....................................................................7
OverviewofCustomFields....................................................................................................................................9
UsingFormulastoSupportProjectVBAMacros.............................................................................................10
DefiningCustomFields........................................................................................................................................11
UsingaValueListinaCustomField...................................................................................................13
UsingaFormulainaCustomField......................................................................................................17
TestingforanNADateValue..............................................................................................................21
UsingGraphicalIndicatorsinaCustomField.....................................................................................21
DefiningCustomOutlineCodes.........................................................................................................................25
DeletingaCustomFieldorOutlineCode.........................................................................................................30
Module02:UsingObjects,Methods,andProperties.........................................................33
UnderstandingtheMicrosoftProjectObjectModel........................................................................................35
ExplainingObjects,MethodsandProperties....................................................................................................37
UsingApplicationRelatedObjects,Methods,andProperties......................................................................37
UsingProjectRelatedObjects,Methods,andProperties...............................................................................39
UsingProjectObjects...........................................................................................................................39
UsingProjectMethods..........................................................................................................................41
UsingProjectProperties.......................................................................................................................41
UsingTaskRelatedObjects,Methods,andProperties...................................................................................42
UsingResourceRelatedObjects,Methods,andProperties...........................................................................43
UsingAssignmentRelatedObjects,Methods,andProperties......................................................................44
UsingtheObjectBrowser.....................................................................................................................................45
Module03:RecordingMacros................................................................................................49
UnderstandingWhentoRecordaMacro...........................................................................................................51
RecordingaMacro.................................................................................................................................................51
ShortcutforRecordingaMacroinProject2010................................................................................................52
ModifyingaRecordedMacro..............................................................................................................................55
StoringYourVBACode........................................................................................................................................56
ControllingtheSizeofProject.MPPFiles........................................................................................................59
RepairingCorruptedFiles....................................................................................................................................60
Module04:LoopingThroughYourSchedule......................................................................61
LoopingThroughAllTasksinaProject............................................................................................................63

iii

Contents
ClearingaCustomFieldforNonSummaryTasks..........................................................................................64
UsingLoopstoSetCustomFieldValues...........................................................................................................64

Module05:CreatingNewObjectsinMicrosoftProject....................................................67
SafelyCreatingNewObjects...............................................................................................................................69
CreatingNewViews,Tables,andFilters...........................................................................................................69
CreatingaNewToolbar........................................................................................................................................70
CreatingaNewMenu............................................................................................................................................71
ThenewProject2010ribbonandmenus...........................................................................................................73
Module06:ManagingRunTimeErrors...............................................................................77
ManagingErrors.....................................................................................................................................................79
UsingResumeNextErrorHandling....................................................................................................81
UsingGotoErrorHandling..................................................................................................................81
SelectinganErrorHandlingMethod....................................................................................................82
UsingtheErrObject.............................................................................................................................82
Module07:CreatingandRunningUserForms....................................................................85
UnderstandingUserForms....................................................................................................................................87
CreatingaUserForm..............................................................................................................................................88
AddingCodetoaUserForm.................................................................................................................................89
Module08:ControllingMicrosoftProjectUsingEvents...................................................91
WhatAreEventsandHowCanTheyHelpYou?.............................................................................................93
UnderstandingProjectEvents..............................................................................................................................93
UnderstandingTaskEvents.................................................................................................................................95
Module09:CreatingtheProjectControlCenterMacro.....................................................99
StructuringYourMacros.....................................................................................................................................101
ProjectControlCenterMacroOverview..........................................................................................................102
DesigningtheProjectControlCenter..................................................................................................102
DeliverablesforProjectControlCenter...............................................................................................103
UnderstandingtheProjectControlCenterCode.................................................................................104
Module10:DisplayingDrivingTasks................................................................................111
DesigningtheDrivingTaskMacro..................................................................................................................113
DesigningtheMainProcedure............................................................................................................114
UnderstandingTaskDependencies......................................................................................................115
UnderstandingtheTaskDriverCode..................................................................................................115
AddingaUserFormtoDisplaytheResults.....................................................................................................123
AddingaToolbartoRuntheMacro.................................................................................................................127

Module11:SharingandDistributingYourMacros.........................................................129
SharingandDistributingyourMacros............................................................................................................131
RunningYourMacrosfromAllProjects..........................................................................................................131
UsingtheGlobal.mptFile..................................................................................................................................131
AutomaticallyMoveMacrosintotheGlobal.mpt..........................................................................................132
CopyingModulesandFormstotheGlobal.mpt..................................................................................132

iv

Contents
AddingtheCopyToGlobalToolbarButton..........................................................................................134
AddingaMacroCommandtotheRibbon............................................................................................134
EditingtheGlobal.mptCodetoCreateaGlobalMacrosToolbar........................................................136

Module12:WorkingwithTimePhasedData....................................................................139
UnderstandingTimePhasedData....................................................................................................................141
ReadingTimePhasedData................................................................................................................................142
ExportingTimePhasedDataintoa.csvfile....................................................................................................144
WritingTimePhasedData.................................................................................................................................146
UpdatingtheProjectControlCenterwithResourceData............................................................................147
DesigningtheGetResourceProcedure............................................................................................................149
Module13:ControllingExcelwithProjectVBA...............................................................153
UsingExcelforProjectReporting.....................................................................................................................155
UnderstandingVBAAutomation......................................................................................................................155
UsingLateBinding..............................................................................................................................155
UsingEarlyBinding............................................................................................................................156
ChangingSecuritytoenableOfficeAutomation...........................................................................................159
UsingVBAtoaddaReferencetoExcel............................................................................................................159
TakingAdvantageofEarlyandLateBinding.................................................................................................160
ConnectingtoanAlreadyOpenCopyofExcel..............................................................................................161
ExportingaListofResourcestoExcel..............................................................................................................162
RunningExcelVBACodeUsingProjectVBA................................................................................................164
ControllingMicrosoftProjectfromExcel........................................................................................................164
CreatingtheSCurvesMacroinExcel..............................................................................................................166
CompletingtheWhoDoesWhatWhenMacro...............................................................................................170
Module14:CreateaCostMarginReport............................................................................177
UnderstandingtheCostMarginReport...........................................................................................................179
Module15:ConsolidatingMultipleProjects.....................................................................185
CreatingConsolidatedProjectReports............................................................................................................187
Module16:ChangingWorkingTime..................................................................................191
WorkingwithCalendars.....................................................................................................................................193
DesigningtheUpdateCalendarsMacro..........................................................................................................193
CreatetheCalendarExceptionsUserForm..........................................................................................194
AddtheTextClasstoStoreCalendarExceptions................................................................................203
ApplyCalendarChangestoAllProjects..............................................................................................205
AddtheMacrototheProjectControlCenterToolbar.........................................................................208
Module17:DisplayPredecessorandSuccessorTasks.....................................................211
WorkingwithTaskDependencies....................................................................................................................213
DesigningtheLinkedTasksMacro...................................................................................................................213
UnderstandingtheTaskDependencyObject..................................................................................................215
UnderstandingtheTaskLinksMacroCode.....................................................................................................215
Module18:MiscellaneousUsefulCode..............................................................................223
IndentingTasksUsingVBA..............................................................................................................................225

Contents
ReorganizingTaskSequences...........................................................................................................................227
CreatingFormulasinCustomFields................................................................................................................228
WorkingwithHyperlinks..................................................................................................................................228
WorkingwithSubprojects..................................................................................................................................230
MakingYourCodeRunFaster...........................................................................................................................231

TimingCodeExecution........................................................................................................................232
UsingtheWindowsAPItoOpenandSaveFiles...........................................................................................233

UsingFileOpen...................................................................................................................................234
UsingFileSaveas................................................................................................................................236
UsingGetFolder..................................................................................................................................236
ApplyingtheclsBrowseClasstoaNewProject..................................................................................237

SECTION2:ReadingandWritingDatawithProjectDatabases...................................239
DevelopingProjectDatabaseCode...................................................................................................................241
DownloadingtheSampleFiles.............................................................................................................241
Module19:ImportingDatafromOtherSources...............................................................243
ImportingDatafromanExcelWorkbook........................................................................................................245
ImportingDatafromanAccessDatabase........................................................................................................253
ImportingDatafromanSQLServerDatabase...............................................................................................255
Module20:ReadingandWritingDatainaProjectDatabase........................................259
UsingthePJDB.HTMFile..................................................................................................................................261
LoopingThroughallProjectsinaDatabase...................................................................................................262
ReadingDatafromaDatabase..........................................................................................................................264
CreatingtheDatabaseView.................................................................................................................264
CreatingtheExcelVBAMacro............................................................................................................265
WritingDatatoaDatabase.................................................................................................................................267
Module21:UsingOLEDB.....................................................................................................273
UnderstandingOLEDB......................................................................................................................................275
UsingtheOLEDBDriverforYourProjectVersion...........................................................................275
OLEDBDriversandProject2007Onwards.......................................................................................276
Connectingtoa.mppFileUsingOLEDB..........................................................................................276
Module22:AccessingProjectServerDatafromExcel.....................................................283
UnderstandingthePJSVRDB.HTMFile..........................................................................................................285
UnderstandingtheProjectServerReportingDatabase.................................................................................286
WeeklyReportingonProjectServerData.......................................................................................................286
LoopingthroughAllProjects...............................................................................................................286
StartingProjectProfessionalandLoggingintoProjectServer..........................................................287
SettinguptheExcelReport..................................................................................................................288
ReadingaListofAllProjectTitles......................................................................................................288
ReadingMilestoneandCurrentTaskData.........................................................................................292
WritingtheVBACodefortheExcelReportMacro.............................................................................293
CreatingaProjectProgramReport....................................................................................................................297

vi

Contents

Module23:UsingProject2007VBA....................................................................................299
IntroducingProject2007VBA............................................................................................................................301
UsingNamedParameters...................................................................................................................................301
UndoingaMacro..................................................................................................................................................302
ManagingCustomFields....................................................................................................................................303
ControllingVisualReports................................................................................................................................304
ManagingCalendars............................................................................................................................................304
FormattingCellBackgroundColors.................................................................................................................305
ManagingDeliverables.......................................................................................................................................306
ConvertingSQLViewsfrom2003to2007.......................................................................................................306
Module24:UsingProject2010VBA....................................................................................311
IntroducingProject2010VBA............................................................................................................................313
UsingNamedParameters...................................................................................................................................313
Project2010VBACompatibility........................................................................................................................314
ControllingtheTimelineView..........................................................................................................................314
DisplayingtheTimelineView..............................................................................................................314
AddingTaskstotheTimelineView.....................................................................................................315
HandlingUserControlledScheduling................................................................................................316
WorkingwiththeTeamPlanner.......................................................................................................................316
MiscellaneousNewVBAMethods...................................................................................................................316
SECTION3:DevelopingAddinsusingVisualStudioforOffice................................319
WhenandWhytoDevelopAddinsforMicrosoftProject..........................................................................321
AboutProjectandAddins.................................................................................................................................321
DeveloperToolsyouwillneed..........................................................................................................................322
SkillsandKnowledgeyouneedtodevelopaProjectAddin.....................................................................322
Module25:IntroducingVSTOProjectAddins................................................................323
IntroducingVSTOProjectAddins..................................................................................................................325
WhatSkillsandKnowledgeyouneedtoKnow.............................................................................................325
WhatyourDevelopmentPCneeds...................................................................................................................325
MethodologyforDevelopinganAddin.........................................................................................................326
ChoosingyourProgrammingLanguage..........................................................................................................326
FileOpencodeinVB.............................................................................................................................327
FileOpencodeinC#forVisualStudio2008........................................................................................327
FileOpencodeinC#forVisualStudio2010........................................................................................328
DifferencesbetweenVBAandVBorC#.........................................................................................................328
CodeStructureDifferences..................................................................................................................328
CodeLocations.....................................................................................................................................329
DeploymentDifferences.......................................................................................................................329
CheckingyourDevelopmentEnvironmentWorks........................................................................................329
SecurityOptionsforAddins.............................................................................................................................331
DisablingandRemovingAddins....................................................................................................................332

vii

Contents

Module26:YourFirstAddin...............................................................................................333
ConvertingaSampleMacrointoanAddin...................................................................................................335
TheVBAMacro...................................................................................................................................335
ConvertingVBAtoVB........................................................................................................................338
ConvertingVBAtoC#2010................................................................................................................342
ReplacingtheUserForm.......................................................................................................................345
ErrorHandlinginAddins.................................................................................................................................348
VSTOandProjectEvents....................................................................................................................................350
ToaddaneventinVB:.........................................................................................................................350
ToaddaneventinC#:.........................................................................................................................351
CreatingaButtonontheProject2010Ribbon....................................................................................352
Developing64BitAddins.................................................................................................................................355
Module27:DeployingyourAddin.....................................................................................357
DeploymentChoicesforAddins......................................................................................................................359
ClickOnceDeployment........................................................................................................................359
WindowsInstallerDeployment...........................................................................................................361
Index.......................................................................................................................................................................363

viii

Vous aimerez peut-être aussi