rename the active document in MS word

How do I rename the active document in MS word using VBA (macros)? I want to save the document with the new name and delete the old name.  TIA
LVL 39
thenelsonAsked:
Who is Participating?
 
GrahamSkanConnect With a Mentor RetiredCommented:
Well this works OK for me:
Sub TestRename()
    Dim strFullName As String
    
    strFullName = ActiveDocument.FullName
    ActiveDocument.SaveAs Replace(strFullName, ".do", "_bak.do")
    Kill strFullName
End Sub

Open in new window

0
 
GrahamSkanRetiredCommented:
You can only change the name by saving it, so you need something like:

ActiveDocument SAveas ActiveDocument.Path & "\" & "NewName.doc"
0
 
thenelsonAuthor Commented:
So I saved the filed under the new name. But now I need to delete the old file. I tried using
   Set fs = CreateObject("Scripting.FileSystemObject")
   fs.DeleteFile TheFileName
but I get "Access denied". Ms Word still has it open so it can't be deleted.

So how do I delete the old file?
0
 
thenelsonAuthor Commented:
When the code gets to
    Kill strFullName
I get "permission denied"

This works for me
    ActiveDocument.Close wdSaveChanges
    Name strFullName As Replace (strFullName, ".doc", " Signed.doc")
0
 
thenelsonAuthor Commented:
Thanks for your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.