A family of Microsoft word processing software products for creating web, email, and print documents.
The following version of the macro shows how you can allow the Date-Picker Content Control's date to have formats like 'ddd MMM d, YYYY' or 'dddd, d MMMM YYYY', for example, and how to lock the output content controls so that no-one can edit them. The code lines for those refinements should be readily apparent.
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Application.ScreenUpdating = False
Dim Rng2 As Range, Rng3 As Range, StrDt As String
With CCtrl
If .Title <> "Date1" Then Exit Sub
StrDt = .Range.Text
If UBound(Split(StrDt, " ")) > 2 Then StrDt = Trim(Replace(StrDt, Split(StrDt, " ")(0), ""))
ActiveDocument.SelectContentControlsByTitle("Date2")(1).LockContents = False
ActiveDocument.SelectContentControlsByTitle("Date3")(1).LockContents = False
Set Rng2 = ActiveDocument.SelectContentControlsByTitle("Date2")(1).Range
Set Rng3 = ActiveDocument.SelectContentControlsByTitle("Date3")(1).Range
If IsDate(StrDt) Then
Rng2.Text = Format(CDate(StrDt) + 30, CCtrl.DateDisplayFormat)
Rng3.Text = Format(CDate(StrDt) + 90, CCtrl.DateDisplayFormat)
Else
Rng2.Text = "": Rng3.Text = ""
If .ShowingPlaceholderText = False Then MsgBox "Not a valid date!", vbExclamation
End If
ActiveDocument.SelectContentControlsByTitle("Date2")(1).LockContents = True
ActiveDocument.SelectContentControlsByTitle("Date3")(1).LockContents = True
End With
Application.ScreenUpdating = True
End Sub
Additional logic could also be added to move the 30 & 90 days so that, if they fall on a weekend, the dates could be shifted to the preceding Friday or following Monday.