[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1579
  • Last Modified:

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
0
thenelson
Asked:
thenelson
  • 3
  • 2
1 Solution
 
GrahamSkanCommented:
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
 
GrahamSkanCommented:
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
 
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now