问题
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!
回答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
来源:https://stackoverflow.com/questions/38877773/is-there-vba-code-to-see-if-enterprise-project-2013-file-is-checked-out-before-o