7

Trying to help our Project 2013 Users out with some VBA code, and we have come to a point where we can't seem to find an answer for finding if a Project 2013 file is checked out on our PWA server using VBA. They basically have a list of Projects set as tasks in a single Project file, and the VBA code loops through the list of tasks to run FileOpenEx, do some changes, and then closes it. However, the need is to be able to check to see if the Project File is checked out prior to running FileOpenEx on each Project in the list. Here is a sample of what I'm going for that doesn't quite do what I want it to.

SelectBeginning
While ActiveCell.CellColor <> pjBlack
   fname = "<>\" & ActiveCell.Task.Name
   justname = ActiveCell.Task.Name

   On Error Resume Next
   If Application.Projects.CanCheckOut(fname) Then '<--This does not work correctly, not checking Enterprise Projects?
       FileOpenEx Name:=fname, ReadOnly=false
       'Do Some stuff
       FileCloseEx Save:=pjSave, CheckIn:=True
       FileSave
   Else
      MsgBox (justname & " can not be checked out")
   End If

   SelectCell Row:=1
Wend

If anyone has a better solution, an easy way to check this, or another workaround to finding out if an Enterprise Project is checked out through VBA code, please let me know. Thanks!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
David Welker
  • 368
  • 2
  • 13
  • are the projects you are looking to see if they are checked out inside a master project ? – Shai Rado Aug 14 '16 at 11:19
  • No, these are not part of a master project. We currently have master projects disallowed. – David Welker Aug 15 '16 at 17:58
  • @David. I think this snippet from Microsoft might help: https://msdn.microsoft.com/en-us/library/office/jj820423.aspx. Basically you want to select the Project from the Projects collection, and check the type of project that it is (enterprise vs not). Then there are some methods at the Project level for checking in and out. – Ryan Wildry Aug 16 '16 at 14:04
  • Thanks Ryan, I've attempted this code before, and it will only check currently opened projects, but will not look through the enterprise projects. – David Welker Aug 16 '16 at 17:26
  • What Project.Type are being returned iterating through each project in projects collection? – Ryan Wildry Aug 16 '16 at 18:19
  • It does not iterate through the projects, it only returns the Project File that is currently opened. If I am able to set the project object to an enterprise Project file by just passing in a project name, I could make this workable. But I have not seen an example of that anywhere either. – David Welker Aug 16 '16 at 19:41

1 Answers1

1

We created a workaround that works for the planners, but we do have to open the file either way. What this will do is open the file in Read Only mode, then attempt to check it out without alerts. Afterwards, if I have it checked out (which means no one else had it checked out), it'll set j=0 and save, then move on to the next project. If someone else has it checked out, then it'll go to 'errorhandler' which tells the project to close without saving, and save the filename in a string to be returned later.

SelectBeginning
While ActiveCell.CellColor <> pjBlack
   fname = "<>\" & ActiveCell.Task.Name
   justname = ActiveCell.Task.Name
   FileOpenEx Name:=fname, ReadOnly=true
   Set ProjToOpen = Application.Projects.Application.ActiveProject
   j = 1
   Application.DisplayAlerts = False
   ProjToOpen.Checkout Project
   Application.DisplayAlerts = True

   If Not Application.IsCheckedOut(ProjToOpen.Name) Then
      GoTo errorhandler
   End If

   'Perform actions here

   j = 0
   FileCloseEx Save:=pjSave, CheckIn:=True
   FileSave
errorhandler: 
   If Not j = 0 Then
      ReDim Preserve skippedfiles(0 to skipped) As String
      skippedfiles(skipped) = justname
      skipped = skipped + 1
      ProjToOpen.Application.FileCloseEx Save:=pjDoNotSave
      GoTo GoToNextProj
   End If

GoToNextProj:
   SelectCell Row:=1

Wend
msgstring = Join(skippedfiles(), vbCr)

MsgBox "Here are the files that were already checked out and therefore not changed: " & vbCr & msgstring
David Welker
  • 368
  • 2
  • 13