Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 03-30-2015, 02:15 PM
canadansk canadansk is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Mar 2015
Posts: 4
canadansk is on a distinguished road
Default Replace checkbox symbol with check box content control

Hi all,
I have a mail merge field in my Word 2010 document that contains a check box control that is checked or not depending on the content from the input list.
{IF NEW = "NEW" "☒" "☐"} where those boxes are actually check box controls
However, after the mail merge is complete, the check box control is replaced with a symbol for the checked or unchecked box, as appropriate. As a result, one can no longer switch from checked to unchecked in the final document as you would for a check box control.
I am looking for a simple way to find the checked or unchecked symbol in the output document and replace it with a check box document control in the appropriate state.


I'm not big on programming, but if you could point me in the right direction, I'll try my best. I have played around with VB Macros in the past (very amateur), so I tried that again and got this far as a "proof of concept":
Code:
Sub Checkbox()
' ChrW(9744) is unchecked box; 9746 is checked box
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = ChrW(9744)
        .Replacement.Text = ChrW(9746)
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.HomeKey Unit:=wdStory
End Sub
I also found out the way to add a checkbox control:
Code:
 Selection.Range.ContentControls.Add (wdContentControlCheckBox)
But I haven't figured out how to marry that last piece of code into the 'replace' line, nor how to define the checkbox as checked or not, based on the search.Thanks for the help.
Reply With Quote
  #2  
Old 03-30-2015, 05:50 PM
gmaxey gmaxey is offline Windows 7 32bit Office 2010 (Version 14.0)
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 750
gmaxey will become famous soon enough
Default

Code:
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
     Wend
   End With
    Set oRng = ActiveDocument.Range
   Selection.Find.ClearFormatting
   With oRng.Find
     .Text = ChrW(9744)
     While .Execute
       Set oCC = ActiveDocument.ContentControls.Add(wdContentControlCheckBox, oRng)
       oCC.Checked = True
       oRng.Collapse wdCollapseEnd
     Wend
   End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 03-31-2015, 01:31 PM
canadansk canadansk is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Mar 2015
Posts: 4
canadansk is on a distinguished road
Default

Hi gmaxey. Thanks for the code. Definitely looks like it's going down the path I need to travel, but I get a runtime error 4198 on the following:

Code:
 
Set oCC = ActiveDocument.ContentControls.Add(wdContentControlCheckBox, oRng)
To attempt to fix it myself, one thing I tried was to drop the ", oRng" portion, in which case the script runs through, but just puts all of the checked and unchecked document controls where my cursor happened to be.

There's a lot of Google hits on runtime error 4198 but this one might be useful?
http://support.microsoft.com/en-us/kb/275558
I'm trying to incorporate that into your script, but, as I said, I haven't used VBA in about 10 years, and even then I was barely scratching the surface.
Reply With Quote
  #4  
Old 03-31-2015, 01:51 PM
canadansk canadansk is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Mar 2015
Posts: 4
canadansk is on a distinguished road
Default

I have attached a sample document that I am running the script on, in case that helps.
Attached Files
File Type: docx mailmerge_checkbox_test.docx (67.6 KB, 9 views)
Reply With Quote
  #5  
Old 03-31-2015, 04:51 PM
gmaxey gmaxey is offline Windows 7 32bit Office 2010 (Version 14.0)
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 750
gmaxey will become famous soon enough
Default

Try this:

Code:
Option Explicit
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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 04-01-2015, 08:21 AM
canadansk canadansk is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Mar 2015
Posts: 4
canadansk is on a distinguished road
Default

Not sure what happened. When I first tried it I got the same error message on the same line. That came up a few times afterwards, after trying a few little things like making sure the document was saved, etc.

Then, I noticed one time that despite the error message, it seemed to have worked! So, I restarted the mail merge process again, created a new document, and ran the script through. No run time error! I have no idea why it went away, but it seems to be working!

Many thanks Greg.
Reply With Quote
Reply

Tags
check box, mail merge, word 2010

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find and replace a symbol with another symbol wondermuse Word 5 10-09-2014 06:30 AM
Deleting a table from a content control -- preserving the content control BrainSlugs83 Word Tables 8 11-14-2013 03:06 AM
Word2010 check boxes and plain text content control boxes in same table fcsungard Word 5 06-01-2012 01:16 AM
Check box symbol jdthelen Word 1 09-07-2009 01:43 PM
Excel ->VB code for Checkbox (control toolbox) kirkstyle Excel 0 08-16-2006 04:17 PM


All times are GMT -7. The time now is 10:39 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft