View Single Post
 
Old 09-11-2019, 10:46 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Create a new paragraph style called "Bookmark_Hidden" and format its font as hidden. Apply it to the bookmark which should then be hidden.

Assuming that the 'fields' are content controls then set the date field to display a valid date and use the following code.

The second macro will randomly select items 1 to 4 in your list box, and you can still select any other item in that list. Frankly I don't see the point of this. Why not just select the item required?

See attached example (note that there are regional issues involved when comparing dates from content controls using this method).


Code:
Option Explicit

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    With ContentControl
        If .ShowingPlaceholderText = False Then
            Select Case .Title
                Case Is = "Date"
                    If IsDate(.Range.Text) = True Then
                        If Format(.Range.Text, "yyyymmdd") > 20190801 Then
                            ActiveDocument.Styles("Bookmark_Hidden").Font.Hidden = False
                        Else
                            ActiveDocument.Styles("Bookmark_Hidden").Font.Hidden = True
                        End If
                    Else
                        MsgBox "Not a valid date!"
                    End If
                Case Else
            End Select
        Else
            ActiveDocument.Styles("Bookmark_Hidden").Font.Hidden = True
        End If
    End With
End Sub

Private Sub Document_Open()
Dim occ As ContentControl
Dim i As Integer
    ActiveDocument.Styles("Bookmark_Hidden").Font.Hidden = True
    ActiveDocument.SelectContentControlsByTitle("Date").Item(1).Range.Text = ""
    Set occ = ActiveDocument.SelectContentControlsByTitle("Type").Item(1)
    occ.DropdownListEntries.Item(Int((5 * Rnd) + 2)).Select
    Set occ = Nothing
End Sub
Attached Files
File Type: docm CheckDate.docm (38.0 KB, 4 views)
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com

Last edited by gmayor; 09-12-2019 at 02:56 AM.
Reply With Quote