Consulting

Results 1 to 10 of 10

Thread: VBA code to name a document

  1. #1

    Unhappy VBA code to name a document

    Hello,

    I have a macro for saving a word document to a specific folder and it works great. The only problem is that i dont know how to name the document correctly. the code is below.

    Sub SaveAFile()
    Dim path As String
    path = "C:\Users\User\Test.docx"
    With ActiveDocument
    .SaveAs FileName:=path
    .Close
    End With
    End Sub
    with this code the document is named Test.docx when its saved to the new folder. I want the document to keep its current name. Can anyone help please.

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Build a path string.

    I don't see code that creates a new folder. I only see a hard-coded path string.

    Try retrieving document name with ActiveDocument.Name
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Thanks, so I replaced =path with ActiveDocument.Name but now the document doesn’t go to the location I want. I’m not sure where it went.

  4. #4
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    You need to build a full path string.

    path = "some folder path\" & ActiveDocument.Name

    Where do you want folder path to come from - user input?
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    So the word document will be open and it will be in my documents folder.The document will have a different name every time. The macro will then save it in "C:\Users\User\Test.docx". at the moment the macro is saving it to "C:\Users\User\Test.docx" but it is naming the document Test.

  6. #6
    I don’t want the code to create a new folder. I want the word document to go to an existing folder which in this example is "C:\Users\User\Test.docx"

  7. #7
    At the moment the macro is sending the document to the correct folder "C:\Users\User\Test.docx" but it is renaming the document test. I want the document to keep its original name

  8. #8
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Sub SaveAFile()
    Dim path As String
    path = "C:\Users\User" & ActiveDocument.Name
    With ActiveDocument
    .SaveAs FileName:=path
    .Close
    End With
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  9. #9
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Don't forget \ before file name. If you don't post code between CODE tags, forum drops that \ character unless you go to Advanced edit and unclick "Automatically parse links in text".


    You have a folder named "User"?
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    That’s worked pretty well thank you

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •