View Single Post
 
Old 03-14-2018, 09:24 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The field code solution proposed by Charles probably isn't suitable in this case, as you're using a date picker content control - not a formfield - and you'd require a ContentControlOnExit macro to trigger the field updates. And, since you'll require such a macro, you'd probably do better to avoid the field coding altogether and instead use a ContentControlOnExit macro in the 'ThisDocument' code module of the document or its template, coded along the lines of:
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Application.ScreenUpdating = False
Dim Dt As Date, StrDt As String
With CCtrl
  If .Title <> "StartDate" Then Exit Sub
  If .ShowingPlaceholderText = True Then
    ActiveDocument.SelectContentControlsByTitle("DateOffset1")(1).Range.Text = "Pending"
    ActiveDocument.SelectContentControlsByTitle("DateOffset2")(1).Range.Text = "Pending"
    ActiveDocument.SelectContentControlsByTitle("DateOffset2")(2).Range.Text = "Pending"
    ActiveDocument.SelectContentControlsByTitle("DateOffset3")(1).Range.Text = "Pending"
  Else
    StrDt = .Range.Text
    If IsDate(StrDt) Then
      Dt = CDate(StrDt)
    Else
      Dt = CDate(Split(StrDt, (Split(StrDt, " ")(0)))(1))
    End If
    ActiveDocument.SelectContentControlsByTitle("DateOffset1")(1).Range.Text = Format(Dt + 7, .DateDisplayFormat)
    ActiveDocument.SelectContentControlsByTitle("DateOffset2")(1).Range.Text = Format(Dt + 14, .DateDisplayFormat)
    ActiveDocument.SelectContentControlsByTitle("DateOffset2")(2).Range.Text = Format(Dt + 14, .DateDisplayFormat)
    ActiveDocument.SelectContentControlsByTitle("DateOffset3")(1).Range.Text = Format(Dt + 21, .DateDisplayFormat)
  End If
End With
Application.ScreenUpdating = True
End Sub
The above code presupposes your date picker content control is titled 'StartDate' and you have three child plain text content controls titled 'DateOffset1', 'DateOffset2', and 'DateOffset3', respectively, with two instances on the 'DateOffset2' content control.

See attached demo.

If you wanted to have offsets that might fall on weekends, but the calculated date should fall on the previous Friday or following Monday, you could use something like:
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Application.ScreenUpdating = False
Dim Dt As Date, StrDt As String, i As Long
With CCtrl
  If .Title <> "StartDate" Then Exit Sub
  If .ShowingPlaceholderText = True Then
    ActiveDocument.SelectContentControlsByTitle("DateOffset1")(1).Range.Text = "Pending"
    ActiveDocument.SelectContentControlsByTitle("DateOffset2")(1).Range.Text = "Pending"
    ActiveDocument.SelectContentControlsByTitle("DateOffset2")(2).Range.Text = "Pending"
    ActiveDocument.SelectContentControlsByTitle("DateOffset3")(1).Range.Text = "Pending"
  Else
    StrDt = .Range.Text
    If IsDate(StrDt) Then
      Dt = CDate(StrDt)
    Else
      Dt = CDate(Split(StrDt, (Split(StrDt, " ")(0)))(1))
    End If
    Select Case Dt Mod 7
      Case 0: i = -1 'Saturday to Friday
      Case 1: i = 1 'Sunday to Monday
      Case Else: i = 0
    End Select
    ActiveDocument.SelectContentControlsByTitle("DateOffset1")(1).Range.Text = Format(Dt + 9 + i, .DateDisplayFormat)
    ActiveDocument.SelectContentControlsByTitle("DateOffset2")(1).Range.Text = Format(Dt + 14, .DateDisplayFormat)
    ActiveDocument.SelectContentControlsByTitle("DateOffset2")(2).Range.Text = Format(Dt + 14, .DateDisplayFormat)
    ActiveDocument.SelectContentControlsByTitle("DateOffset3")(1).Range.Text = Format(Dt + 23 + i, .DateDisplayFormat)
  End If
End With
Application.ScreenUpdating = True
End Sub
The above example provides for the calculated date to become Friday if it otherwise falls on a Saturday, and to become Monday if it otherwise falls on a Sunday.

To make both dates Friday, change:
Case 1: i = 1 'Sunday to Monday
to:
Case 1: i = -2 'Sunday to Friday

To make both dates Monday, change:
Case 0: i = -1 'Saturday to Friday
to:
Case 0: i = 2 'Saturday to Monday
Attached Files
File Type: docm Content Control Date Calculations.docm (40.3 KB, 28 views)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote