简体   繁体   中英

VBA EXCEL/WORD - SAVE AS

This is probably a really easy question, but here what I have I have a load of code that works fine but then when it comes to saving the document I can only get it to save as a specific name, but I want it to save as "Visitors Diary Recruitment (something unique)" so that it doesn't overwrite the document each time I run it and instead creates a new document.

With wApp
    .ActiveDocument.SaveAs2 (path)
    .ActiveWindow.Close
    .Quit

    Set wApp = Nothing
    Set wDoc = Nothing
End With

End Sub

You could save the document with a unique Id (there are many options for this - depending on your need):

  • Random number
  • Use a datetime stamp
  • Use a Guid

Using a random number:

set uniqueName = Int(25 * Rnd()) + 1 //25 is the amount of random numbers you want

Using a DateTime:

set uniqueName = Format(Now(), "MMMM dd, yyyy hh:mm AM/PM")

Using a Guid - I havn't done this myself yet, although i'm certain there must be a way to generate one. ( This might help)

Then change your SaveAs to this:

ActiveDocument.SaveAs2 ("C:\Users\colesa\Desktop\Recruitment Macros\Visitor Diary Recruitment" +uniqueName + ".doc")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM