Consulting

Results 1 to 4 of 4

Thread: Help using command button to save a word form

  1. #1

    Help using command button to save a word form

    Hi

    I am trying to create a button within a form on word that once clicked automatically saves the form into a specificshared location. I am also trying to autosave it with a specific file name. Inthis case it would be the first row of the form (a number followed by the dateat which the document was submitted/saved. So far I have worked out how to open a save as dialog box and create afilename as my username and the date. Ihave used the following code for this however as mentioned its not quite what Ineeded. Is there anyone here that hasdone this before or who can help me?
    Thanks

  2. #2
    Assuming that the 'number' is a content control titled 'Number' then the following should save the document in the user's documents folder. with the name e.g. 12345-20191112.docx

    Sub SaveForm()
    Dim strPath As String
    Dim oCC As ContentControl
        strPath = Environ("USERPROFILE") & Chr(92) & "Documents\"
        Set oCC = ActiveDocument.SelectContentControlsByTitle("Number").Item(1)
        If oCC.ShowingPlaceholderText = True Then
            Beep
            MsgBox "Complete the missing information!"
            oCC.Range.Select
            GoTo lbl_Exit
        Else
            ActiveDocument.SaveAs2 FileName:=strPath & oCC.Range.Text & Format(Date, "-yyyymmdd") & ".docx"
        End If
    lbl_Exit:
        Set oCC = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thanks gmayor

    When I add this code I get the following error

    Run-Time error '5941'
    The requested member of the collection does not exist

    Do I need to remove the existing Private Sub CommandButton1_click() and replace with Sub SaveForm()
    I am guessing this may be part of the issue? Also if I need to save the form somewhere other than "Documents" do I replace that?
    Thanks

  4. #4
    The error suggests you don't have a content control titled 'Number'. As my reply indicates the macro grabs the number from such a content control. As we have no idea how your form is configured and content controls are the best method of creating a form, it seemed a reasonable assumption. If you want to save it somewhere else, change the path in strPath to that location (and don't forget the final backslash!)

    If you want to run it from your button add the line SaveForm to your button code to call the macro.

    You may find https://www.gmayor.com/insert_content_control_addin.htm useful to create, modify or convert your form.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

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
  •