We help IT Professionals succeed at work.

rename the active document in MS word

thenelson
thenelson asked
on
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
Comment
Watch Question

GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

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

ActiveDocument SAveas ActiveDocument.Path & "\" & "NewName.doc"
CERTIFIED EXPERT

Author

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?
Retired
CERTIFIED EXPERT
Top Expert 2012
Commented:
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

CERTIFIED EXPERT

Author

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")
CERTIFIED EXPERT

Author

Commented:
Thanks for your help!