Thread: [Solved] Make a field required
View Single Post
 
Old 07-27-2022, 04:55 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I'm saying you can't have the same macro name twice in the same module. In this case the macro name is critical because the name and location means it automatically runs whenever the user moves out the selection out of a Content Control.

But you can combine the functionality in both macros into a single macro.

The following code should replace everything you had in the earlier posts
Code:
Private Sub Document_ContentControlOnExit(ByVal aCC As ContentControl, Cancel As Boolean)
  Dim aDate As Date
  If aCC.ShowingPlaceholderText Then
    Select Case aCC.Tag
      Case "Rev. #", "PDR #", "Date of Discovery", "Type"   'these CCs require an answer
        MsgBox "This Content Control requires a response.", vbInformation + vbOKOnly, "INPUT REQUIRED"
        Cancel = True
    End Select
  Else    'if CC has content, check its type matches the content
    If aCC.Tag Like "*[#]" Then     'if the CCs tag ends with '#'
      If Not IsNumeric(aCC.Range.Text) Then
        MsgBox "This field requires a numeric response.", vbInformation + vbOKOnly, "INPUT REQUIRED"
        Cancel = True
      End If
    ElseIf aCC.Tag Like "*Date*" Then   'if CCs tag includes 'Date'
      If Not IsDate(aCC.Range.Text) Then
        MsgBox "This field requires a date response.", vbInformation + vbOKOnly, "INPUT REQUIRED"
        Cancel = True
      ElseIf aCC.Title = "Date of Initiation" Then    'note tag doesn't match on this CC in your doc
        aDate = CDate(aCC.Range.Text) + 30
        ActiveDocument.SelectContentControlsByTitle("Due Date")(1).Range.Text = aDate
      End If
    End If
  End If
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia

Last edited by Charles Kenyon; 07-28-2022 at 02:04 PM.
Reply With Quote