Answered by:
Closing the resource pool file

Question
-
I'd like to be able to close the resource pool file before running certain parts of my code.
To do so, I can loop through the open files and close the one's that are a pool. Project can identify this, because if you click on the "share resources" button you get a different dialog window if the file is a pool.
Question is: How to identify this with VBA?
(or any other method to close the resource pool)
Thanks!
Andre
Tuesday, March 20, 2018 4:50 PM
Answers
-
Andre,
If your goal is to detect and then close the pool file, an indirect approach will work nicely. The following code uses that fact that a pool file has no tasks, or at least should not have any tasks, so it simply checks all open files, finds the one without tasks and closes it. You will need to add a Save constant to the FileClose Method but otherwise it should do what you need.
John
Sub ClosePool()
Dim prj As Project
For Each prj In Application.Projects
If prj.Tasks.Count = 0 Then FileClose
Next prj
End Sub- Edited by John - Project Tuesday, March 20, 2018 7:55 PM grammar
- Marked as answer by Huismu5 Thursday, March 22, 2018 10:57 AM
Tuesday, March 20, 2018 7:54 PM
All replies
-
Andre,
If your goal is to detect and then close the pool file, an indirect approach will work nicely. The following code uses that fact that a pool file has no tasks, or at least should not have any tasks, so it simply checks all open files, finds the one without tasks and closes it. You will need to add a Save constant to the FileClose Method but otherwise it should do what you need.
John
Sub ClosePool()
Dim prj As Project
For Each prj In Application.Projects
If prj.Tasks.Count = 0 Then FileClose
Next prj
End Sub- Edited by John - Project Tuesday, March 20, 2018 7:55 PM grammar
- Marked as answer by Huismu5 Thursday, March 22, 2018 10:57 AM
Tuesday, March 20, 2018 7:54 PM -
Thank you for your reply John. Your idea will work. Meanwhile, I was experimenting some more and came up with the following, which closes all sharer projects (even if an ignorant PM did something wrong in the pool or shared resources from a project).
Sub PoolClose()
Dim res As Resource
Dim strPoolName As String
Dim i As Integer
Dim imax As Integer
For Each res In ActiveProject.Resources
i = 1
imax = Projects.Count
strPoolName = res.Project & ".mpp"
If strPoolName <> ActiveProject.Name Then
Do While i <= imax
With Application
If .Projects(i).Name = strPoolName Then
.Projects(i).Activate
FileClose
If Projects.Count <= 1 Then Exit Sub
i = imax + 1
End If
End With
i = i + 1
Loop
End If
Next res
End Sub
- Edited by Huismu5 Wednesday, March 21, 2018 3:54 PM typos
Wednesday, March 21, 2018 3:51 PM -
Andre,
You're welcome and thanks for the feedback.
John
Thursday, March 22, 2018 4:46 PM -
Even simpler by using the ResourcePoolName property
Sub PoolClose2()
Dim strPoolName As String
Dim i As Integer, imax As Integer
strPoolName = ActiveProject.ResourcePoolName
i = 1
imax = Projects.Count
Do While i <= imax
With Application
If .Projects(i).FullName = strPoolName Then
.Projects(i).Activate
FileClose
Exit Sub
End If
End With
i = i + 1
Loop
End Sub
Friday, March 23, 2018 10:35 AM -
Andre,
I don't see how it is simpler than the code I provided but if it works better for you, then go with it.
John
Friday, March 23, 2018 3:15 PM