Consulting

Results 1 to 8 of 8

Thread: Replace form checkbox with content control checkbox

  1. #1

    Replace form checkbox with content control checkbox

    Hi,

    I don't have much understanding of MS Word macros, but I am trying to create a macro that would replace all my form checkboxes with content control checkboxes. The newly replaced checkboxes should reflect the old state of the legacy checkboxes. Note that in the document both types of checkboxes can be found and only the legacy ones must be replaced (I have attached an example).
    Replace checkboxes.docx
    Replace checkboxes.docx
    I've found the following code and I tried to adjust it to work for my case, but I keep getting errors:

    Sub Checkbox()Dim oRng As Word.Range
    Dim oCC As ContentControl
       'ChrW(9744) is unchecked box; 9746 is checked box
       Set oRng = ActiveDocument.Range
       Selection.Find.ClearFormatting
       With oRng.Find
         .Text = ChrW(9744)
         .Forward = True
         .Wrap = wdFindStop
         .Format = False
         .MatchCase = False
         .MatchWholeWord = False
         .MatchWildcards = False
         .MatchSoundsLike = False
         .MatchAllWordForms = False
         While .Execute
           Set oCC = ActiveDocument.ContentControls.Add(wdContentControlCheckBox, oRng)
           oCC.Checked = False
           oRng.Collapse wdCollapseEnd
           oRng.Move wdCharacter, 2
         Wend
       End With
        Set oRng = ActiveDocument.Range
       Selection.Find.ClearFormatting
       With oRng.Find
         .Text = ChrW(9746)
         While .Execute
           Set oCC = ActiveDocument.ContentControls.Add(wdContentControlCheckBox, oRng)
           oCC.Checked = True
           oRng.Collapse wdCollapseEnd
           oRng.Move wdCharacter, 2
         Wend
       End With
    lbl_Exit:
      Exit Sub
    End Sub
    Any tips or help is highly appreciated!Replace checkboxes.docx

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    If your document already has formfields other than checkboxes, you really shouldn't be replacing the formfield checkboxes with content control checkboxes. Formfields and content controls were not designed to be used together in the same document. Trying to do so is a known source of usability problems.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Quote Originally Posted by macropod View Post
    If your document already has formfields other than checkboxes, you really shouldn't be replacing the formfield checkboxes with content control checkboxes. Formfields and content controls were not designed to be used together in the same document. Trying to do so is a known source of usability problems.
    Unfortunately, one of the requests is to replace all checkboxes with content control checkboxes for 6.5k MS Word documents... I've been trying to use a 3rd party automation software for this, but it does not seem to be stable enough and that is why I am trying a Macro solution.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    The point remains, though, that formfields and content controls should not be used in the same document. Whoever dreamt up this requirement needs to understand the usability implications and, moreover, that any macros attached to formfield checkboxes in the documents concerned will cease to function once that change is made.

    The code to replace formfield checkboxes with content control checkboxes isn't at all complicated, but I would be reluctant to provide it without those responsible for the change accepting up-front their responsibility for potentially breaking 6.5k MS Word documents...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    123
    Location
    If the only legacy formfields are the checkboxes, the problem is not there. Paul knows what he is talking about, though. You would want to be getting rid of all legacy formfields, not just the checkboxes.

    Also, I can see doing this with templates, but why with documents? They should stay in their current state, not used to create new documents. This is one of those situations where it sounds like the customer is asking for the wrong thing. You should be providing templates with Content Controls based on old documents, not modifying legacy documents.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Chas Kenyon View Post
    If the only legacy formfields are the checkboxes, the problem is not there.
    Even if the only legacy formfields are the checkboxes, if they have on-exit macros attached, the change will break whatever checking/unchecking those checkboxes was supposed to do...
    Quote Originally Posted by Chas Kenyon View Post
    You would want to be getting rid of all legacy formfields, not just the checkboxes.
    The same goes for any other formfields with the 'calculate on exit' option checked and/or on-exit macros attached.
    Last edited by macropod; 09-06-2022 at 06:08 AM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    123
    Location
    Quote Originally Posted by macropod View Post
    Even if the only legacy formfields are the checkboxes, if they have on-exit macros attached, the change will break whatever checking/unchecking those checkboxes was supposed to do...

    The same goes for any other formfields with the 'calculate on exit' option checked and/or on-exit macros attached.
    Agreed.

  8. #8
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Sub ConvertFFtoCC()
    Dim oFld As FormField
    Dim oRng As Range
    Dim strName As String, bValue As Boolean
    Dim oCC As ContentControl
      For Each oFld In ActiveDocument.Range.FormFields
        If oFld.Type = 71 Then
          Set oRng = oFld.Range
          oRng.Select
          bValue = oFld.CheckBox.Value
          strName = oFld.Name
          oFld.Delete
          Set oCC = ActiveDocument.ContentControls.Add(8, Selection.Range)
          oCC.Checked = bValue
          oCC.Title = strName
        End If
      Next
    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
  •