Log in

View Full Version : Use checkbox state to format text (Word macro)



Ornthorpe
09-07-2012, 01:13 PM
I'm trying to use a Word macro to format text in a table. The macro is triggered by entry into a checkbox in the table.
It works but I need to be able to also determine whether or not the checkbox is checked so if user unchecks it, I can format back to previous state. I have a checkbox on each row that formats that row upon entry so it would be great if I could use the same macro for many of the checkboxes.
VBA:
' --Get and store "checkbox name"
' Unprotect the file--
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
--Check to see if "checkbox name" checkbox is checked
--If checked then
--Do this formating
else
--Do different formating
' Reprotect the file--
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
End Sub

macropod
09-07-2012, 04:19 PM
Unless you store the 'previous' state (and that may mean storing Styles and/or hard formatting foe each an every character), there is no way unchecking the checkbox can do any more than changing the format from, say, bold to italics for a specified portion of the range.

Ornthorpe
09-09-2012, 11:36 AM
Understood. I want the entry macro on all checkboxes to do exactly the same thing (formatting) every time > look for cell shading red, if red, make green, look for green, if green make red. So that if not checked > check and make green, if checked, uncheck and make red.

macropod
09-09-2012, 06:48 PM
There are already some threads here that deal with conditionally shading table cells. Have you looked at those? See, for example:
http://www.vbaexpress.com/forum/showthread.php?t=23876

Cross-posted at: http://www.techsupportforum.com/forums/f57/format-with-checkbox-macro-665026.html
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

Ornthorpe
09-10-2012, 09:06 AM
Thanks Paul. The vb referred to seems to evaluate the entire table at one time. I need to shade a specific row when the user clicks in a cell (checkbox) on that row so that they immediately see that when they check Yes or No that row is shaded green or red. The user is only allowed clicks. no text entry as another system identifies who they are and records that information.

macropod
09-10-2012, 05:24 PM
The vb referred to seems to evaluate the entire table at one time.
Actually, the code in the link processes all cells/rows (depending on which sub you're looking at) individually, because that is what it was meant to do. It doesn't simply shade the whole table, though. As coded, it simply meant you could same code for the on-exit macro for multiple cells/rows, rather than having a separate macro for each one.

In any event, that was just one example. There are others if you take the time to look for them.

Ornthorpe
10-03-2012, 03:28 PM
Got it working:
VBA:
Sub CheckYes2Green()
'
' CheckYesGreen Macro
'
Dim i As Integer
For i = 1 To 6
If ActiveDocument.FormFields("c" & i).CheckBox.Value = True Then
' Unprotect the file
ActiveDocument.Unprotect Password:=""
Selection.SelectRow
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = 5296274
Selection.MoveRight Unit:=wdCharacter, Count:=3
' Reprotect the file
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
Else
' If ActiveDocument.FormFields("c" & i).CheckBox.Value = False Then
' Unprotect the file
ActiveDocument.Unprotect Password:=""
Selection.SelectRow
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = 721354855
Selection.MoveRight Unit:=wdCharacter, Count:=3
' Reprotect the file
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
Next
End Sub

macropod
10-03-2012, 03:43 PM
Hi Ornthorpe,

Glad you got it sorted.

When posting code, please use the VBA tab to enclose it in the formatting tags. That way, it'll all be correctly formatted.