Expanding from Greg's code. If you add a plain text content control titled 'Days' then put the following in the ThisDocument module of the document, the calculation will be automatic.
Code:
Option Explicit
Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
Dim oCCS As ContentControl
Dim oCCE As ContentControl
Dim oCCD As ContentControl
Set oCCS = ActiveDocument.SelectContentControlsByTitle("Start").Item(1)
Set oCCE = ActiveDocument.SelectContentControlsByTitle("End").Item(1)
Set oCCD = ActiveDocument.SelectContentControlsByTitle("Days").Item(1)
Select Case ContentControl.Title
Case "Start"
If oCCD.ShowingPlaceholderText = False And oCCE.ShowingPlaceholderText = False Then
MsgBox "Make your change then click outside the field to update the day count."
End If
Case "End"
If oCCE.ShowingPlaceholderText = False And oCCE.ShowingPlaceholderText = False Then
MsgBox "Make your change then click outside the field to update the day count."
End If
If oCCS.ShowingPlaceholderText = True Then
MsgBox "Complete the start date field before clicking here"
GoTo lbl_Exit
End If
Case "Days"
If oCCS.ShowingPlaceholderText = True Or oCCE.ShowingPlaceholderText = True Then
MsgBox "Complete both date fields before clicking here"
GoTo lbl_Exit
Else
oCCD.Range.Text = CStr(fcnCalcDays(oCCS.Range, oCCE.Range))
End If
Case Else
End Select
lbl_Exit:
Exit Sub
End Sub
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim oCCS As ContentControl
Dim oCCE As ContentControl
Dim oCCD As ContentControl
Set oCCS = ActiveDocument.SelectContentControlsByTitle("Start").Item(1)
Set oCCE = ActiveDocument.SelectContentControlsByTitle("End").Item(1)
Set oCCD = ActiveDocument.SelectContentControlsByTitle("Days").Item(1)
Select Case ContentControl.Title
Case "Start"
If oCCD.ShowingPlaceholderText = False And oCCE.ShowingPlaceholderText = False Then
oCCD.Range.Text = CStr(fcnCalcDays(oCCS.Range, oCCE.Range))
End If
If Val(oCCE.Range.Text) < Val(oCCS.Range.Text) And _
oCCE.ShowingPlaceholderText = False Then
MsgBox "The start date is later than the end date?"
oCCE.Range.Text = ""
oCCD.Range.Text = ""
GoTo lbl_Exit
End If
Case "End"
If Val(oCCE.Range.Text) < Val(oCCS.Range.Text) And _
oCCS.ShowingPlaceholderText = False Then
MsgBox "The start date is later than the end date?"
oCCE.Range.Text = ""
oCCD.Range.Text = ""
GoTo lbl_Exit
End If
If oCCD.ShowingPlaceholderText = False And oCCS.ShowingPlaceholderText = False Then
oCCD.Range.Text = CStr(fcnCalcDays(oCCS.Range, oCCE.Range))
End If
Case Else
End Select
lbl_Exit:
Set oCCS = Nothing
Set oCCE = Nothing
Set oCCD = Nothing
Exit Sub
End Sub
Function fcnCalcDays(Date1 As Range, Date2 As Range) As Long
Dim lngDays As Long
Dim lngIndex As Long
Dim dTest As Date
lngDays = 0
dTest = Date1.Text
For lngIndex = 1 To DateDiff("d", Date1.Text, Date2.Text)
If Weekday(dTest, vbUseSystemDayOfWeek) <> 6 Then
lngDays = lngDays + 1
End If
dTest = DateAdd("d", 1, dTest)
Next
fcnCalcDays = lngDays
lbl_Exit:
Set oCCS = Nothing
Set oCCE = Nothing
Set oCCD = Nothing
Exit Function
End Function
Sub ClearDateFields()
Dim oCCS As ContentControl
Dim oCCE As ContentControl
Dim oCCD As ContentControl
Set oCCS = ActiveDocument.SelectContentControlsByTitle("Start").Item(1)
Set oCCE = ActiveDocument.SelectContentControlsByTitle("End").Item(1)
Set oCCD = ActiveDocument.SelectContentControlsByTitle("Days").Item(1)
oCCD.Range.Text = ""
oCCE.Range.Text = ""
oCCS.Range.Text = ""
lbl_Exit:
Set oCCS = Nothing
Set oCCE = Nothing
Set oCCD = Nothing
Exit Sub
End Sub