PDA

View Full Version : Solved: find and replace checkboxes in Word document



GaryMN
01-16-2012, 07:11 PM
Looking for macro to find checkboxes and replace them with text.

I have some long documents that were designed with lots of checkboxes. I want to write a macro to replace every checkbox with [__].

I have tried many ways, without success.

When I have the document open and highlight the checkbox, the properties shows "Check43".

How can I write a macro that finds all of these checkboxes and replaces them with [__]?

Thanks. Gary

GaryMN
01-16-2012, 08:40 PM
More:

In some of the documents, the checkbox is a form field; if you copy and paste it into the "find" space, it shows as just an O. If you press Alt+F9 the fields show {FORMCHECKBOX}. If you copy and paste this into find-replace, it still records as just an O. Also, if you enter the expression ^19 formcheckbox in find-replace, it still just enters an O.

To correct an error in my first posting, I should clarify that if you highlight and click "properties," sometimes it shows field settings Bookmark Check43, Check box enabled; other places "Check6."

In other documents I have to work with, there is a checkbox, but it is not a field, just a square; when you enter it into the "find" space, it shows as an unrecognized character--a rectangle.

I cannot figure out how to find and replace just within the Word document using find-replace. All the more, I cannot figure out how to write this into an automated VB macro for either form--the one with the form field or the one with the rectangle symbol.

I did find a macro for deleting the checkboxes that works to delete all checkboxes in the field-populated documents [not what I want]:
Sub DeleteCheckboxFormFields()
Dim ff As FormField
For Each ff In ActiveDocument.FormFields
With ff
If .Type = wdFieldFormCheckBox Then
.Delete
End If
End With
Next ff
End Sub

This finds and deletes all instances, but I can't seem to reach my goal of replacing by manipulating this macro. I have a feeling there is a simple answer--something about changing the .Delete to something else.

All help greatly appreciated. Thanks, Gary

macropod
01-17-2012, 03:52 AM
Hi Gary,

Try:
Sub ChkBxClr()
Dim i As Long, Rng As Range
With ActiveDocument
For i = .FormFields.Count To 1 Step -1
With .FormFields(i)
If .Type = wdFieldFormCheckBox Then
Set Rng = .Range
.Delete
Rng.Text = "[__]"
End If
End With
Next
Set Rng = Nothing
End With
End Sub
PS: When posting code, please use the VBA tags.

GaryMN
01-17-2012, 07:19 AM
Thanks Paul. This works great!

YossiD
03-12-2014, 11:21 PM
Paul,

I'm trying to do something similar, except I want to search for checkboxes followed by a space and replace with checkboxes followed by a tab. Can you help?

Thanks,

YossiD

macropod
03-12-2014, 11:40 PM
The change to the original code is trivial:

Sub ChkBxTab()
Dim i As Long
With ActiveDocument
For i = .FormFields.Count To 1 Step -1
With .FormFields(i)
If .Type = wdFieldFormCheckBox Then
.Range.Characters.Last.Next = vbTab
End If
End With
Next
End With
End Sub
PS: Please don't resurrect old threads; start a new one with a link to the old one if applicable.

YossiD
03-13-2014, 01:12 AM
That works nicely and I can even more or less follow the code, but it doesn't do quite what I need.

This example deletes whatever character follows a checkbox and replaces it with a tab. I want to find instances of a checkbox followed by a space and replace with a checkbox followed by a tab. I think that would be a somewhat different procedure.

Thanks



The change to the original code is trivial:

Sub ChkBxTab()
Dim i As Long
With ActiveDocument
For i = .FormFields.Count To 1 Step -1
With .FormFields(i)
If .Type = wdFieldFormCheckBox Then
.Range.Characters.Last.Next = vbTab
End If
End With
Next
End With
End Sub
PS: Please don't resurrect old threads; start a new one with a link to the old one if applicable.

macropod
03-13-2014, 01:34 AM
Maybe I'm missing something but I can't see any difference between: (a) replacing the space after a checkbox with a tab; and (b) replacing a checkbox and space after it as a set with a checkbox (e.g. the same checkbox) and a tab as a set. The outcome is to all intents the same, except that (b) requires more work.

YossiD
03-13-2014, 06:57 AM
Some of my checkboxes are in narrow table cells with no space (or any other character) following. The current code adds a tab after the checkbox causing wrapping to the next line. I prefer the macro to skip these checkboxes.

Also, the more generalized case will be more educational for me.

Thanks

InkMing
03-13-2014, 09:09 AM
Paul,

For grins what if we wanted to go the other way? Replace a certain piece of text say, [__] with Checkboxes?

macropod
03-13-2014, 01:45 PM
Some of my checkboxes are in narrow table cells with no space (or any other character) following. The current code adds a tab after the checkbox causing wrapping to the next line. I prefer the macro to skip these checkboxes.
The only checkboxes that tabs are being inserted after are those that spaces following. The problem, therefore, is that you have spaces after the checkboxes in those cells. I suggest you remove them before running the macro - or delete the unwanted tabs after the macro has run.

macropod
03-13-2014, 01:46 PM
For grins what if we wanted to go the other way? Replace a certain piece of text say, [__] with Checkboxes?
Yes, that's possible, but I have better things to do with my time than to write code 'for grins'.

InkMing
03-13-2014, 01:58 PM
Figured out a way to do it without code. Just do find and replace and highlight the checkbox, copy it, and then put in ^c into the replace field.