rename the active document in MS word

Posted on 2012-08-16
Last Modified: 2012-12-23
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
Question by:thenelson
    LVL 76

    Expert Comment

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

    ActiveDocument SAveas ActiveDocument.Path & "\" & "NewName.doc"
    LVL 39

    Author Comment

    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?
    LVL 76

    Accepted Solution

    Well this works OK for me:
    Sub TestRename()
        Dim strFullName As String
        strFullName = ActiveDocument.FullName
        ActiveDocument.SaveAs Replace(strFullName, ".do", "")
        Kill strFullName
    End Sub

    Open in new window

    LVL 39

    Author Comment

    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")
    LVL 39

    Author Closing Comment

    Thanks for your help!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
    A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
    This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
    This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now