Results 1 to 12 of 12
  1. #1
    Star Lounger
    Join Date
    Oct 2011
    Location
    Auckland. New Zealand
    Posts
    61
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Defaulting file name when user saves Word document

    I want to 'help' users save Word documents using the correct file naming convention.

    I've developed a template and now I want the user to be 'prompted' with a suggested file name when they Save or Save As.

    The file name will be made up of information within the document, such as the company name, date, author, etc. To do this, I set up Macrobutton NoMacro (Sorry, I'm new and don't know all the lingo) prompts and bookmarked the fields. So the theory is that when they click and enter the company name, I can use the bookmark to see what they've entered.

    So I guess I have 2 questions:
    1. Will I be able to access information (such as company name) using the bookmark theory I've outlined above? Or will I have to set up a UserForm or something?
    2. How do I 'default' the file name?

    Thanks for your help.

  2. #2
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts
    1. Yes, a macro that's going to create a file name can get the contents of bookmarks, with an expression like ActiveDocument.Bookmarks("Company").Range.Text where Company is the name of the bookmark. However, there's a small gotcha with your plan: You must insert the bookmark so there's at least one space on either side of the MacroButton NoMacro field. If you don't, then when the user clicks on the field and types, both the field and the bookmark will be overwritten -- and then the macro won't have anything to refer to. The space on either side will make it less likely that the bookmark will be invisibly erased (although users can still clobber them in other ways).

    You don't "need" a UserForm, but a UserForm can be much more user-friendly, robust (able to validate answers before putting them into the document text), and flexible than anything that can be done within the document text. Read http://gregmaxey.mvps.org/word_tip_p..._userform.html.

    2. Use the code in http://www.word.mvps.org/FAQs/Macros...efFilename.htm to put the suggested file name into the Title property, which Word uses to suggest an initial file name.

  3. #3
    Star Lounger
    Join Date
    Oct 2011
    Location
    Auckland. New Zealand
    Posts
    61
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hi and thanks heaps for your reply. I did have a look at the file name document (on the word.mvps website), but I got confused by it. Unfortunately our file naming convention includes underscores - or maybe I should say it used to! Basically my problem with this sample is that I don't know where to put the code.

    Do I need just the code in the ChangeProps subroutine? Will this automatically activate when the user 'saves' or 'saves as'?

    I got really excited because I found a piece of code on the internet that did exactly what I wanted, except that when I inserted it into my code and compiled it, I found out that it was written for Excel and used functions that weren't valid in Word.

  4. #4
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts
    I apologize for pointing to an article that isn't specific enough and, in any event, is not applicable to the current versions of Word (at least, it doesn't work in Word 2010). The following code will work in any version of Word, and it doesn't have a problem with underscores or other separators (if you want some other character, change the assignment of the variable uscore).

    Code:
    ' A macro with the specific name FileSave is called
    ' whenever the user saves a document attached to
    ' this template.
    Sub FileSave()
        If ActiveDocument.Path = "" Then
            ' If the document has never been saved, the
            ' value of its .Path is an empty string; otherwise
            ' it has the file's path and name.
            With Dialogs(wdDialogFileSaveAs)
                .Name = MakeDocName  ' call the function below
                .Show                ' the suggested name will be in the dialog
            End With
        Else
            ' The document has already been saved with a name
            ' so just save it there.
            ActiveDocument.Save
        End If
    End Sub
    
    Function MakeDocName() As String
        Dim theName As String
        Dim uscore As String
        uscore = "_"
        
        With ActiveDocument.Bookmarks
            ' Use any combination of bookmarks and
            ' document properties to assemble the
            ' suggested name.
            theName = Trim(.Item("company").Range.Text)
            theName = theName & uscore & Trim(.Item("date").Range.Text)
            theName = theName & uscore & _
                Trim(ActiveDocument.BuiltInDocumentProperties("author"))
        End With
        
        MakeDocName = theName  ' return the assembled name
    End Function

  5. #5
    Star Lounger
    Join Date
    Oct 2011
    Location
    Auckland. New Zealand
    Posts
    61
    Thanks
    8
    Thanked 0 Times in 0 Posts
    It works! It works!! I had to make some minor changes (because there was some other information that had to be included in the file name), but I've got there and it's working well. Thanks so much for your help. I particularly liked your use of comments that allowed me to follow what was happening.

    I'm not wanting to do it at this stage, but is it also possible to default the file location?

  6. #6
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts
    Quote Originally Posted by Niki B View Post
    It works! It works!! I had to make some minor changes (because there was some other information that had to be included in the file name), but I've got there and it's working well. Thanks so much for your help. I particularly liked your use of comments that allowed me to follow what was happening.
    Great! That's what we're here for, helping you to learn to help yourself.

    I'm not wanting to do it at this stage, but is it also possible to default the file location?
    Sure, just include the full path to the desired folder at the beginning of the theName string. For example,

    Code:
    theName = "C:\temp\" & Trim(.Item("company").Range.Text)
    will cause the Save As dialog to open to the C:\temp folder, with the suggested name in the filename box.

  7. The Following User Says Thank You to jjfreedman For This Useful Post:

    Niki B (2012-08-13)

  8. #7
    Star Lounger
    Join Date
    Oct 2011
    Location
    Auckland. New Zealand
    Posts
    61
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Actually, I do have one small challenge that I forgot about in yesterday's excitement. This works brilliantly when I click on Save or Ctrl + S. It doesn't work when I click on Save As, or Close (and then select Save).

    Any ideas why not?

  9. #8
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Niki,

    Create the following:

    Sub FileSaveAs()

    FileSave

    End Sub

    This will take care of the SaveAs problem.
    The Close problem is more complicated. Maybe someone else more familiar with Word VBA can help out.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  10. The Following User Says Thank You to RetiredGeek For This Useful Post:

    Niki B (2012-08-13)

  11. #9
    Star Lounger
    Join Date
    Oct 2011
    Location
    Auckland. New Zealand
    Posts
    61
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thanks for your help. The code works great. I had a few small challenges with the bookmark going AWOL but I've sorted that now. I have to change the code slightly for SaveAs as my user wants to be able to 'reuse' the document (and the data within it) to create another version, but I'm sure I can sort that out.

  12. #10
    Star Lounger
    Join Date
    Oct 2011
    Location
    Auckland. New Zealand
    Posts
    61
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Sample file name.jpg

    Back again. I've found out that if a user either presses F11 or clicks on my bookmark, enters data and then presses the Tab button (in error, instead of F11), my default file name contains spaces.

    I am using the function "Trim" - theName = theName & uscore & Trim(.Item("CompanyName").Range.Text).

    Is there any way to remove the spaces (generated by the Tab button) from the file name (see my attachment). Both me and my tester have done this accidentally (pressed Tab) so it needs to be sorted as I can't save the file with the spaces in the name.

    Also.....

    I worked out that I can use FileClose to work out my default file name, but if I do this, no surprises here, the user can't use Close to close the document - it can only be closed by clicking on the cross in the top right hand corner. Any ideas on how to fix this (or am I just trying to be tooo clever)?

  13. #11
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    You could use the Replace string function to remove spaces
    theName = Replace(theName, " ", "")

    To remove tabs from a string
    theName = Replace(theName, vbTab, "")

    Can you show us the code you are using for FileClose? There will be a way around this problem but we need to see what you are doing first.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  14. #12
    Star Lounger
    Join Date
    Oct 2011
    Location
    Auckland. New Zealand
    Posts
    61
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Post

    ARRRGGGHHH - I can't believe I didn't think of just replacing the spaces.

    Currently my FileClose code is just saving the document - I did try various versions of ActiveDocument.Close but they didn't work.

    My code for Closing is:
    Sub FileClose()
    SaveAsCheck = True
    WorkOutFileName
    End Sub

    Sub WorkOutFileName()

    If ActiveDocument.Path = "" Then
    ' If the document has never been saved, the
    ' value of its .Path is an empty string; otherwise
    ' it has the file's path and name.
    With Dialogs(wdDialogFileSaveAs)
    .Name = MakeDocName ' call the function below
    .Show ' the suggested name will be in the dialog
    End With
    Else
    If SaveAsCheck = True Then
    With Dialogs(wdDialogFileSaveAs)
    .Name = MakeDocName ' call the function below
    .Show ' the suggested name will be in the dialog ***
    End With
    Else
    ' The document has already been saved with a name
    ' so just save it there.
    ActiveDocument.save
    End If
    End If

    I use "SaveAsCheck" because my users have the practice of using existing versions of documents and saving them for a different client, meaning the file name may have to be 'recompiled' even if a path already exists. Does that makes sense?

    While I was getting my thoughts together to respond, I quickly opened an existing document (based on the template), changed just one field, and pressed File, Save As. I got an error in the line marked with ***. (.show)

    Thanks again for your help Andrew.

Posting Permissions

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