Results 1 to 20 of 81

Thread: How to change Date Picker content control format based on date comparison

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #27
    It works with the following configuration. So, date picker content controls on the title page mapped from the DIP (insert > quick parts > document property) each with its individual style (FromDate and ToDate). These styles are cross referenced (StyleRef) on the introduction page. This works with the following code:

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)    Application.ScreenUpdating = False
        Dim i As Long
        Dim Str1 As String, Str2 As String
        With ContentControl
            If (.Title = "Visit Date - From") Or (.Title = "Visit Date - To") Then
                If .Range.Text = .PlaceholderText Then Exit Sub
                If .Title = "Visit Date - From" Then
                    .Range.ParentContentControl.DateDisplayFormat = "D MMMM YYYY"
                    Str1 = .Range.Text
                    With ActiveDocument.SelectContentControlsByTitle("Visit Date - To")(1)
                        .Range.ParentContentControl.DateDisplayFormat = "D MMMM YYYY"
                        Str2 = .Range.Text
                    End With
                Else
                    With ActiveDocument.SelectContentControlsByTitle("Visit Date - From")(1)
                        .Range.ParentContentControl.DateDisplayFormat = "D MMMM YYYY"
                        Str1 = .Range.Text
                    End With
                    Str2 = .Range.Text
                End If
                If Format(Str1, "YYYYMMDD") > Format(Str2, "YYYYMMDD") Then
                    ActiveDocument.SelectContentControlsByTitle("Visit Date - To")(1).Range.Text = ""
                    ActiveDocument.SelectContentControlsByTitle("Visit Date - From")(1).Range.Text = ""
                    MsgBox "'Visit Date - From' is greater than 'Visit Date - To'" & _
                    vbCr & vbTab & "Please re-input the correct dates", vbCritical
                ElseIf Format(Str1, "YYYYMMDD") = Format(Str2, "YYYYMMDD") Then
                    ActiveDocument.SelectContentControlsByTitle("Visit Date - To")(1).Range.Text = ""
                    ActiveDocument.SelectContentControlsByTitle("Visit Date - From")(1).Range.Text = ""
                    MsgBox "'Visit Date - From' is the same as 'Visit Date - To'" & _
                    vbCr & vbTab & "Please re-input the correct dates", vbCritical
                Else
                    With ActiveDocument.SelectContentControlsByTitle("Visit Date - From")(1)
                        If Split(Str1, " ")(2) = Split(Str2, " ")(2) Then
                            If Split(Str1, " ")(1) = Split(Str2, " ")(1) Then
                                .Range.ParentContentControl.DateDisplayFormat = "D"
                                '.Range.ParentContentControl.DateDisplayFormat = "D' - '"
                            Else
                                .Range.ParentContentControl.DateDisplayFormat = "D MMMM"
                                '.Range.ParentContentControl.DateDisplayFormat = "D MMMM' - '"
                            End If
                        End If
                    End With
                 End If 
            End If
         End With
        Application.ScreenUpdating = True
    End Sub
    However, I could not get the field codes (StyleRef, Bookmark Ref, etc.) to work when embedded in text or rich text controls. The template crashes with the following message:

    Word found unreadable content in “Technical Report Template.dotm”. Do you want to recover the contents of this document? If you trust the source of this document, click yes?
    The main drawback with this setup is that the cross references on the introduction page can be deleted by the user easily and are not updated automatically (only by F9 or ensuring update fields before printing is checked). Perhaps the update can be achieved by the FileSave() macro.

    Once again, thanks macropod for your code and time.

    Regards,

    JDS_916
    Last edited by JDS_916; 03-30-2014 at 05:30 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •