Consulting

Results 1 to 2 of 2

Thread: CheckBox 'greyed' out/Custom Document Property

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    54
    Location

    CheckBox 'greyed' out/Custom Document Property

    Hi

    This is something that is probably really quite simple but I've never come across it before.

    I have four checkboxes within a single frame on a userform. Anytime I've added checkboxes before they would automatically show up as 'false', i.e. blank.

    On this occasion however, when I call up the Userform, they're appearing as greyed-out and with a tick in them. Is there a reason for this? Have I unintentionally done something that I didn't think I'd done?

    Thanks

    Update: As is always the case, the moment I hit 'post' inspiration struck.

    I have various Custom Document Properties which hold the values of the comboboxes in the Userform and recall them each time the file is opened. I had tried to do the same with the checkbox values using

    If Not (IsEmpty(ActiveDocument.CustomDocumentProperties("Second"))) Then
                 Dim iSecond As String
                      If ActiveDocument.CustomDocumentProperties("Second") <> 0 Then
                          iSecond = ActiveDocument.CustomDocumentProperties("Second")
                          Me.CheckBox1.Value = iSecond
                      Else
                          Me.ComboBox1.Value = ""
                      End If
               End If
    This was why the boxes were greyed out with ticks in them. If I remove the above code all is well.

    My question is now, how can I use a Custom Document Property to store the value of the checkbox and recall it? Will this still work as it does with my other Custom Document Properties?

    ActiveDocument.CustomDocumentProperties.Add _Name:="Second", LinkToContent:=False, Value:="", _
    Type:=msoPropertyTypeString
    Dav
    Last edited by DavG63; 12-03-2015 at 05:38 AM.

  2. #2
    You can write the value by calling a simple macro from the userform e.g.

    SetCustProperty "Second", CheckBox1.Value
    Sub SetCustProperty(strPropertyName As String, bValue As Boolean)
    Dim oProp As DocumentProperty
    Dim bProp As Boolean
    
        For Each oProp In ActiveDocument.CustomDocumentProperties
            If oProp.name = strPropertyName Then
                oProp.Value = bValue
                bProp = True
                Exit For
            End If
        Next oProp
        If Not bProp Then
            With ActiveDocument.CustomDocumentProperties
                .Add name:=strPropertyName, _
                     LinkToContent:=False, _
                     Type:=2, _
                     Value:=bValue
            End With
        End If
    lbl_Exit:
        Exit Sub
    End Sub
    Reading the value into the checkbox is essentially a reversal of the process

    Checkbox1.Value = ReadCustProperty("Second")
    Function ReadCustProperty(strPropertyName As String) As Boolean
    Dim oProp As DocumentProperty
    Dim bProp As Boolean
        For Each oProp In ActiveDocument.CustomDocumentProperties
            If oProp.name = strPropertyName Then
                bProp = oProp.Value
                Exit For
            End If
        Next oProp
        ReadCustProperty = bProp
    lbl_Exit:
        Exit Function
    End Function
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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