Consulting

Results 1 to 3 of 3

Thread: Bookmarked CheckBox

  1. #1
    VBAX Newbie
    Joined
    Feb 2015
    Posts
    1
    Location

    Bookmarked CheckBox

    I have a very old Word document that has a broken macro that I am trying to fix but I have limited VBA knowledge.
    Currently there are Legacy Forms checkmark boxes used in the following macro:

    Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
    Dim BMRange As Range
    Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
    BMRange.Text = TextToUse
    ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
    End Sub
    Private Sub showBookmarks()
    Dim d As Document
    Set d = Documents.Add("C:\Users\Owner-PC\Desktop\Test1.docm")
    'Dim b As Bookmark
    'Set b = d.Bookmarks(




    'Deck Preparation
    If (ActiveDocument.FormFields("deckscope2c").CheckBox.Value = False) Then
    ActiveDocument.Bookmarks("deckscope2").Range.Delete
    End If

    The problem is that I can't check those checkmark boxes without the properties box coming up and I also can't tab through the boxes so I would like to change them to Command (Content) Control checkmark boxes which I can tab to and check off. Unfortunately when I tried to change the macro to read:

    If (ActiveDocument.CommandControls("deckscope2c").CheckBox.Value = False) Then
    ActiveDocument.Bookmarks("deckscope2").Range.Delete
    End If

    It gives me a runtime error 438: Object doesn't support this property or method.

    Any suggestions would be appreciated

  2. #2
    The reason you cannot tab through the legacy form is that it is not protected for forms. Legacy forms must be protected for forms.

    With that protection in place you could use something like the following to delete the checkbox if it is unchecked.

    Dim bProtected As Boolean
        If ActiveDocument.FormFields("deckscope2c").CheckBox.Value = False Then
            'Unprotect the file
            If ActiveDocument.ProtectionType <> wdNoProtection Then
                bProtected = True
                ActiveDocument.Unprotect Password:=""
            End If
            ActiveDocument.FormFields("deckscope2c").Range.Delete
            'Reprotect the document.
            If bProtected = True Then
                ActiveDocument.Protect _
                        Type:=wdAllowOnlyFormFields, _
                        NoReset:=True, _
                        Password:=""
            End If
        End If
    If you have changed the form fields to content controls and have titled the content controls then to delete the "deckscope2c" content control if it is unchecked

    Dim oCC As ContentControl
    Dim oRng As Range
        For Each oCC In ActiveDocument.ContentControls
            If oCC.Title = "deckscope2c" Then
                If oCC.Checked = False Then
                    Set oRng = oCC.Range
                    oCC.Delete
                    oRng.Delete
                End If
            End If
        Next oCC
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    To delete a titled CC you can address it explicitly:

    Sub ScratchMacro()
    Dim oRng As Word.Range
    'A basic Word macro coded by Greg Maxey
      If Not ActiveDocument.SelectContentControlsByTitle("deckscope2c").Item(1).Checked Then
        Set oRng = ActiveDocument.SelectContentControlsByTitle("deckscope2c").Item(1).Range
        ActiveDocument.SelectContentControlsByTitle("deckscope2c").Item(1).Delete
        oRng.Delete
      End If
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.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
  •