PDA

View Full Version : Help using command button to save a word form



Dan Abnormal
11-12-2019, 06:44 AM
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

gmayor
11-12-2019, 07:59 AM
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

Dan Abnormal
11-13-2019, 02:45 AM
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

gmayor
11-13-2019, 05:25 AM
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 (http://www.gmayor.com/insert_content_control_addin.htm) useful to create, modify or convert your form.