Results 1 to 9 of 9
  1. #1
    New Lounger
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    MS Word 2010: VBA script to save Word doc with unique prefix and default name [SOLVED]

    Hello All

    Total newbie to VB, so this probaly sounds like a stupid question.

    i have created a userform which lots of choices that will show / hide these options via bookmarks. I have also worked out how to have a text box on the userform for the customer name and get VBA to do find and replace the text <customer> in the word doc with the details entered in the textfield of teh userform.

    So now, what i would like to do is save the word document as the customer name (entered in the textfield of the userform) with a generic title i.e. "<customer> product brochure".docx "Mr Smith Product Brochure.docx" but cant seem to find answer for word

    I hope that makes sense and that someone can help

    I have attached an example of the above

    many thanks in advance

    Bartrum
    Attached Files Attached Files
    Last edited by RetiredGeek; 2014-07-16 at 05:13. Reason: Marked Solved

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,235
    Thanks
    202
    Thanked 796 Times in 729 Posts
    Bartrum,

    Welcome to the Lounge as a new poster!

    Here's some code I used in a Word Template with VBA forms. When the Exit button was pressed it executed this as part of the Exit subroutine.
    Code:
       With ActiveDocument
    
         '*** Adjust the formula for zFileName to use your parameters.
           zFileName = "A:\" & _
                       Left(.Variables("FirstName").Value, 1) & _
                       Left(.Variables("MiddleName").Value, 1) & _
                       Left(.Variables("LastName").Value, 1) & _
                       " Adult Ed Registration Form"
       End With      '*** Active Document ***
       With Application
           .ScreenUpdating = False
           .DisplayAlerts = False
           On Error Resume Next
           ActiveDocument.SaveAs FileName:=zFileName
           .Quit SaveChanges:=wdDoNotSaveChanges
       End With
    HTH
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  3. #3
    New Lounger
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi RetiredGeek

    My thanks for the quick response and code

    Unfortuantley this does not work for me as I get an error of "Object has been deleted" - I sort of understand why and the customer name textbox on the user form doesnt save the new customer name. I have tried place the code in various places and end up with the same error.

    How do I get round this, or is this not possible?

    thank you again for any help that you can provide

    Bartrum

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,235
    Thanks
    202
    Thanked 796 Times in 729 Posts
    Bartrum,

    I couldn't find a user form attached to your example file?
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  5. #5
    New Lounger
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi RetiredGeek

    odd - but i have re upload a new renamed version eample v2. i have tested that this now works correctly and Alt & F4 brings the user form up again

    thanks for looking into this for me

    Bartrum
    Attached Files Attached Files

  6. #6
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,235
    Thanks
    202
    Thanked 796 Times in 729 Posts
    Bartrum,

    The file still contains no user forms!
    Bartram.JPG

    and Alt+F4 is the Window Close key {close active window}!

    Have you saved your form in Normal vs this document?

    BTW: Your file is a .docx and it needs to be a .dotm if it contains macros or userforms!
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  7. #7
    New Lounger
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    HI again

    3rd time lucky

    yeah i know Alt F4 is to close any activate window but this is whht i set to, to recall the user form, will change this in the final version



    Bartrum
    Attached Files Attached Files

  8. #8
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,235
    Thanks
    202
    Thanked 796 Times in 729 Posts
    Bartrum,

    Ok, here's my tested code modified with your form info. Please read the comments in the code and adjust accordingly.
    Code:
    Private Sub Submit_click()
    
       Dim zFileName  As String
    
    '*** I don't think you want to be calling click events from here!
    '*** They should only be called by the object they are attached to.
    
    '        Call Product1_Click
    '        Call Product2_Click
    '        Call Product3_Click
    '        Call Product4_Click
    '        Call Accessory1_Click
    '        Call Accessory2_Click
    '        Call Accessory3_Click
    '        Call Accessory4_Click
    '        Call Accessory5_Click
    '        Call Accessory6_Click
    '        Call Accessory7_Click
    '        Call Accessory8_Click
    '        Call Customerchange
            
            ActiveDocument.TablesOfContents(1).Update
        
        With ActiveDocument
    
         '*** Adjust the formula for zFileName to use your drive/path parameters. ***
           zFileName = "G:\BEKDocs\" & Customer.Value & _
                       ".docm"  '*** Note if you want to save document w/o macros,
                                '*** so it can't be changed change type to .docx"
        End With      '*** Active Document ***
        
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            On Error Resume Next
            ActiveDocument.SaveAs FileName:=zFileName
            
    '*** Following line Commented out for testing or if you don't want to exit word!
    '        .Quit SaveChanges:=wdDoNotSaveChanges
        End With   'Application
       
    End Sub   'Submit_Click
    Also I would highly suggest you place the line: Option Explicit
    at the top of each of your modules as this will catch any variable name/object name errors before you get to run the code. Of course you'll have to DIM all your variables before you use them but it will help you learn to code as I found several hanging references in your code for the form. I'd suggest you compare the code in the attached version of your document to that which you posted so you can see the necessary changes. BTW: the Alt+F4 is stored in your normal.doc and did not transfer with your document.

    HTH
    Attached Files Attached Files
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  9. #9
    New Lounger
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    MS Word 2010: VBA script to save Word doc with unique prefix and default name [SOLVED]

    Hi RetiredGeek

    You have resolved the issue for me, which is great news!! many many thanks

    Thank you for the addtional advice, which will certainly be taken note of as these are all valuble tips and best pratices for a novice. This has been a great learning expiernce for me

    Have fun

    Bartrum
    Last edited by bartrum; 2014-07-15 at 20:55.

Posting Permissions

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