none
Deleting/Moving a Unlinked Subproject File

    Pertanyaan

  • Okay, do I've found plenty of questions/discussions about deleting/moving files via VBA, but nothing seems to be working for me.

    Basically, I'm trying to setup a "override project" function that will unlink the existing subproject, delete the task, then move the file into a "Removed" directory and create the replacement subproject. Everything works except the moving. Whether I try to actually move the file or copy/delete it, I get the same error:

    Run-time error '70' Permission denied

    Trying to manually delete the file immediately after clearing the error I get:

    The action can't be completed because the file is open in Project 2016

    If I understand the errors correctly, the file, despite being unlinked from the parent project and having that project saved to ensure the change takes effect, is still locked by the Project memory.

    Is there a way force it to close the subproject to allow for it's move/deletion?

    Here're the two main versions of my code I've tried:

    Dim fs As Object
    Dim proj As Subproject
    Dim subTask As Task
    Dim tarProjPath As String
    
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set proj = ActiveProject.Subprojects(5) ' Fixed Selection for testing only
    Set subTask = ActiveProject.Tasks(27) ' Fixed Selection for testing only
    tarProjPath = proj.Path
    
    ' Remove the existing project from the Master Tracker
    proj.LinkToSource = False
    subTask.Delete
    ActiveProject.SaveAs Name:=ActiveProject.FullName
    
    ' Sanity Test Output
    Debug.Print "Target: " & tarProjPath
    Debug.Print "New: " & remPath & Format(Now(), "yyyymmddThhmmss.") & fs.GetBaseName(tarProjPath) & ".mpp"
    
    fs.CopyFile tarProjPath, remPath & Format(Now(), "yyyymmddThhmmss.") & fs.GetBaseName(tarProjPath) & ".mpp" ' Executes fine
    fs.DeleteFile tarProjPath ' Errs
    Dim fs As Object
    Dim proj As Subproject
    Dim subTask As Task
    Dim tarProjPath As String
    
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set proj = ActiveProject.Subprojects(5) ' Fixed Selection for testing only
    Set subTask = ActiveProject.Tasks(27) ' Fixed Selection for testing only
    tarProjPath = proj.Path
    
    ' Remove the existing project from the Master Tracker
    proj.LinkToSource = False
    subTask.Delete
    ActiveProject.SaveAs Name:=ActiveProject.FullName
    
    ' Sanity Test Output
    Debug.Print "Target: " & tarProjPath
    Debug.Print "New: " & remPath & Format(Now(), "yyyymmddThhmmss.") & fs.GetBaseName(tarProjPath) & ".mpp"
    
    fs.MoveFile tarProjPath, remPath & Format(Now(), "yyyymmddThhmmss.") & fs.GetBaseName(tarProjPath) & ".mpp" ' Errs

    Rabu, 13 Juni 2018 19.09

Jawaban

  • Angel,

    After some tweaks to your code and a little testing, this works for me (i.e. no errors).

    John

    Sub test()
    Dim fs As Object
    Dim proj As Subproject
    Dim subTask As Task
    Dim tarProjPath As String

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set proj = ActiveProject.Subprojects(2) ' Fixed Selection for testing only
    Set subTask = ActiveProject.Tasks(1) ' Fixed Selection for testing only
    tarProjPath = proj.Path
    remPath = "C:\Users\John\Desktop\"

    ' Remove the existing project from the Master Tracker
    proj.LinkToSource = False
    subTask.Delete
    Application.FileSaveAs Name:=ActiveProject.FullName

    ' Sanity Test Output
    Debug.Print "Target: " & tarProjPath
    Debug.Print "New: " & remPath & Format(Now(), "yyyymmddThhmmss.") & fs.GetBaseName(tarProjPath) & ".mpp"

    fs.CopyFile tarProjPath, remPath & Format(Now(), "yyyymmddThhmmss.") & fs.GetBaseName(tarProjPath) & ".mpp" ' Executes fine
    fs.DeleteFile tarProjPath ' Errs
    End Sub

    Kamis, 14 Juni 2018 17.28

Semua Balasan

  • Angel,

    I'm having trouble trying to understand what name you are trying to use when you attempt to save the active project after the unlink and deletion.

    ActiveProject.SaveAs Name:=subProj.FullName

    Neither code snippet defines the "subProj" object.

    John

    Rabu, 13 Juni 2018 21.27
  • Sorry, that was suppose to have been replaced with ActiveProject. I forgot to change it after I'd fixed it when testing the simplified code. I've updated the original post. ^^'

    Basically, I put in the save in the hope that would release the subproject file to be moved/deleted, but it didn't.
    Rabu, 13 Juni 2018 21.58
  • Angel,

    After some tweaks to your code and a little testing, this works for me (i.e. no errors).

    John

    Sub test()
    Dim fs As Object
    Dim proj As Subproject
    Dim subTask As Task
    Dim tarProjPath As String

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set proj = ActiveProject.Subprojects(2) ' Fixed Selection for testing only
    Set subTask = ActiveProject.Tasks(1) ' Fixed Selection for testing only
    tarProjPath = proj.Path
    remPath = "C:\Users\John\Desktop\"

    ' Remove the existing project from the Master Tracker
    proj.LinkToSource = False
    subTask.Delete
    Application.FileSaveAs Name:=ActiveProject.FullName

    ' Sanity Test Output
    Debug.Print "Target: " & tarProjPath
    Debug.Print "New: " & remPath & Format(Now(), "yyyymmddThhmmss.") & fs.GetBaseName(tarProjPath) & ".mpp"

    fs.CopyFile tarProjPath, remPath & Format(Now(), "yyyymmddThhmmss.") & fs.GetBaseName(tarProjPath) & ".mpp" ' Executes fine
    fs.DeleteFile tarProjPath ' Errs
    End Sub

    Kamis, 14 Juni 2018 17.28
  • So small a change, using Application.FileSaveAs instead of Project.SaveAs, but that did it. Thank you, once again, for your help!

    Kamis, 14 Juni 2018 19.29
  • Angel,

    You're welcome and thanks for the feedback.

    When I tried it with your line of code

    ActiveProject.SaveAs Name:=ActiveProject.FullName

    as soon as the line was executed code execution stopped (i.e. sub ended). So I tried the Application.FileSaveAs Method but execution still ended without execution the remaining lines of code. Then I realized I was trying to run the code from within the master file itself. Once I got my process straightened out to run the code from the Global, it all came together.

    John

    Kamis, 14 Juni 2018 20.11
  • Odd, it didn't stop the execution of the code for me, it just produced the permission error. In the version that was doing the copy/delete, the file was successfully copied after the SaveAs, but it would err with the DeleteFile.
    Kamis, 14 Juni 2018 20.24