'Kill' command does not work when the document is being closed

Hello,

The below code works when the document is open and saved from the toolbar; however, it does not work when the document is closing and saved from the pop-up prompt. The code is called by the DocumentBeforeSave event.

OldFile = ActiveDocument.Path & "\" & ActiveDocument.Name
If OldFile <> strTmpName & ".docx" Then
    ActiveDocument.SaveAs strTmpName
    Kill OldFile
Else
    ActiveDocument.save
End If
End Sub

Open in new window

I receive Run time error 70 (permission denied) for the 'Kill OldFile' command.

I am using this code to extract information from the document which is reflected in the documents name, so the name of the document is never static. Any direction would be greatly appreciated!

Brock
cksm4Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PandaPantsCommented:
My first thought is that you're getting tripped up by the position of the code, which you say is called by the DocumentBeforeSave event. If the document has not yet been saved--but the Save is pending--then the document is in use and can't be Killed.

In any event (no pun intended), you're implicitly setting the OldFile variable to match the ActiveDocument object, and as long as that ActiveDocument object exists, you won't be able to Kill it.
0
cksm4Author Commented:
Thanks for the response. The 'ActiveDocument.SaveAs strTmpName' saves the document so (I am thinking) there should not be a pending save when the Kill command is reached. The 'OldFile' should at that point be a separate file which is not open (still thinking... yet confused).
0
PandaPantsCommented:
You may be right, except that Saving the document and Closing it are two different things.

Setting aside the fact that we're running in a DocumentBeforeSave event (which worries me because the next thing that should happen when that event code completes is that the process proceeds to the actual Save, regardless of whether we've done an interim Save in the DocumentBeforeSave event code), just Saving the document leaves it open and active, just no longer in a "dirty" state; the ActiveDocument.Saved property is now True, but the document is still in use.

I'm still trying to sort it out in my own head, so I'm going to copy your code and then try to track the possible values in-line:

' Assume ActiveDocument is c:\temp\MyDoc.docx
' Assume also that strTmpName = "c:\temp\MyTmpDoc" (total guess, here)
OldFile = ActiveDocument.Path & "\" & ActiveDocument.Name
' OldFile = "c:\temp\MyDoc.docx"
If OldFile <> strTmpName & ".docx" Then
    ' This should be true, because OldFile = "c:\temp\MyDoc.docx" and strTmpName = "c:\temp\MyTmpDoc" plus ".docx":
    ActiveDocument.SaveAs strTmpName
    ' The ActiveDocument, c:\temp\MyDoc.docx, as just been saved to c:\temp\MyTmpDoc (no file extension)
    Kill OldFile
    ' Attempting to kill "c:\temp\MyDoc.docx".
    ' Okay, now I see: If the SaveAs completed, then Kill OldFile should work.
Else
    ActiveDocument.save
End If
End Sub


Okay, as I wrote in my last inline comment, if the SaveAs completed, then the Kill OldFile command should work. That's what you were trying to say.

In that case, we should look at the possibility that the SaveAs command has NOT completed by the time the Kill command is executing. I've actually run into this problem quite a bit with Word over the years, going all the way back to Word97. Sometimes all it takes is a little programmatic pause to force VBA to wait until the last command is finished before moving on. I have had some success in those situations using something like this:

OldFile = ActiveDocument.Path & "\" & ActiveDocument.Name
If OldFile <> strTmpName & ".docx" Then
    ActiveDocument.SaveAs strTmpName

    dim iCtr as integer
    For iCtr = 1 to 25
        If ActiveDocument.Saved Then
             ' Verify the document name: You'll know best what value strTmpName should be (i.e., whether it includes the path and file extension)
             If ActiveDocument.Name = strTmpName Then
                 GoTo SaveCompleted
             End If ' ActiveDocument.Name = strTmpName
        End If ' ActiveDocument.Saved
    Next iCtr
    ' If the loop completed 25 times and the doc still isn't saved, then pop up a message box:
    MsgBox Prompt:="ActiveDocument.Name = " & ActiveDocument.Name & vbcrlf & vbcrlf & _
         "Should be = " & strTmpName, Buttons:=vbOkOnly

SaveCompleted:
    Kill OldFile

Else
    ActiveDocument.save
End If
End Sub



Ironically, if it really is a timing issue, then even if it loops through 25 times and then gives you the message that the filename didn't change, it will probably complete the operation while it's waiting for you to click through the message box, and then the Kill OldFile command will complete successfully. (That's why I didn't write code to skip past it even if it failed.)

Obviously, this isn't a complete solution, but it may help you to figure out exactly where the weakness is. Good luck.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

cksm4Author Commented:
I had tried a timer earlier in the day thinking the same things but found that the document was closing before the timer ran (if I was even doing it right). So maybe you are onto something here... it may also explain why the code works when the document is saved and left open. I will try this in the morning and HOPEFULLY it works! Stand by.
0
cksm4Author Commented:
I placed the 'Kill' command in the event after where the sub is called which renames and saves the document. This fixed the issue. The timing idea did not work... but it was with a try. Thanks!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cksm4Author Commented:
I determine a workaround
0
PandaPantsCommented:
Seems reasonable, and I'm glad cksm4 found a solution. I'm not trolling for points here (250 is more than enough!), but I would like to point out that each of my first two suggestions began by saying that the problem was likely the position of the Kill command, which didn't belong in the DocumentBeforeSave event, but should actually be moved to an event (unspecified) AFTER that event. In the end, that's exactly the workaround that cksm4 found: "I placed the 'Kill' command in the event after where the sub is called which renames and saves the document. This fixed the issue."

So, we all ended up on the same page! I'll settle for success, any day. ;~)
0
cksm4Author Commented:
To clarify, the 'Kill' command was in a sub called by the DocumentBeforeSave event. this caused the error. I moved the 'Kill' command to the DocumentBeforeSave event after it called the sub which renames the file and this fixed the issue.

No idea why it works one way and not the other... but it does!
0
PandaPantsCommented:
Okay, that makes sense. Again, glad the problem is solved!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.