On Submit, Prefill another field to True RRS feed

  • Question

  • I have a template in Word 2010. After the user fills out the template they click on the Submit button.  The Submit button does a few things.  It will check fields to make sure they are filled, saves the document and then creates a PDF.  I would like it to do one more thing.

    I have a document property called "InvestigationSubmitted."  It has 2 values, True and False.   On Submit, I would like this field to automatically default to True.

    I have no clue how to write the VBA.  Any help is ALWAYS appreciated!!

    Thank you in advance.

    Thursday, March 23, 2017 5:47 PM

All replies

  • This is what the code is currently on the submit button.  I just want to add on to the code as mentioned above. I am using another document property in the template and it is in bold below.  The other fields are legacy.  I tried doing this myself and it was a fail.

    Private Sub cmdSubmit_Click()
     With ActiveDocument.FormFields("RelatedRegulation")
            If .Result = "" Then
                MsgBox "Enter a value in the Related Regulation field first! Click inside the highlighted field.", vbExclamation
                Selection.GoTo what:=wdGoToBookmark, Name:="RelatedRegulation"
                Exit Sub
            End If
        End With
    With ActiveDocument.FormFields("ActivityTableDate")
            If .Result = "" Then
                MsgBox "Enter the Date in the Date Activity table field first! Click inside the highlighted field.", vbExclamation
                Selection.GoTo what:=wdGoToBookmark, Name:="ActivityTableDate"
                Exit Sub
            End If
        End With

    With ActiveDocument.FormFields("Time")
            If .Result = "" Then
                MsgBox "Enter the Time in the Time Activity table field first! Click inside the highlighted field.", vbExclamation
                Selection.GoTo what:=wdGoToBookmark, Name:="Time"
                Exit Sub
            End If
        End With
    With ActiveDocument.FormFields("Activity")
            If .Result = "" Then
                MsgBox "Enter the Activity in the Activity table field first! Click inside the highlighted field.", vbExclamation
                Selection.GoTo what:=wdGoToBookmark, Name:="Activity"
                Exit Sub
            End If
        End With
    Set cc = ActiveDocument.SelectContentControlsByTitle("DateInvCompleted").Item(1)
    If Not (IsDate(cc.Range.Text)) Then ' no date
    MsgBox "Enter the Date Investigation Completed in the Date Investigation Completed field first! Click inside the highlighted field.", vbExclamation
    Exit Sub
            End If
    With ActiveDocument.FormFields("Dropdown")
            If .Result = "Select Result from Dropdown" Then
                MsgBox "Please select a result from the dropdown list!", vbExclamation
                Selection.GoTo what:=wdGoToBookmark, Name:="Dropdown"
                Exit Sub
            End If
        End With
    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
            Replace(ActiveDocument.FullName, ".docx", ".pdf"), _
            ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
            wdExportOptimizeForPrint, Range:=wdExportAllDocument, Item:= _
            wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=True, _
            CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=True, UseISO19005_1:=False
    End Sub

    Friday, March 24, 2017 11:55 AM
  • You say "a document property called "InvestigationSubmitted" but the highlighted line in your code is actually populating a *content control*. If the document has been loaded from a SharePoint Server then the content of that control may well be the type of Property that you can set in a SharePoint folder (these are sometimes called COntent Properties, Server Properties or MetaProperties in Word).

    If you actually have a Content Control in your document that has title InvestigationSubmitted, then to set its value, all you should need to do is something like

    ActiveDocument.SelectContentControlsByTitle("InvestigationSubmitted")(1).Range.Text = "True"

    That assumes it is a plain text content control. If you actually have a checkbox and you want to check it, you would use

    ActiveDocument.SelectContentControlsByTitle("InvestigationSubmitted")(1).Checked= True

    If your document came from Sharepoint and the Content Control corresponds to a property set up on SharePoint in this document's folder, then in the first example, the property should also be set to True, automatically. However, since you are then saving as PDF I do not think that is likely to be relevant.

    Peter Jamieson

    Monday, March 27, 2017 11:32 AM
  • You are correct.  The document is coming from SharePoint.  I am getting the following run-time error:

    The requested member of the collection does not exist. 

    Tuesday, March 28, 2017 5:50 PM
  • That suggests that the document does not actually have a content control called "InvestigationSubmitted". Does it?

    Or are you saying that your code also needs to create a a content control with that name?

    Peter Jamieson

    Saturday, April 1, 2017 12:40 PM
  • I have that control in my document.  So yes, I have it in my document as well as my code.  I am going to check the properties of that control in SharePoint now.
    Wednesday, April 5, 2017 5:26 PM
  • Scacci: I saw your post, but I do not always get notifications from these groups. So if you post again, all I can say is I'll probably see it eventually.

    Peter Jamieson

    Wednesday, April 5, 2017 9:34 PM