Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
Macro to delete text associated with any unchecked check boxes
I have created a document with 10 paragraphs/lines. Each line has a check
box form field and a city's name next to it. I would like to create a macro that can be run that deletes any cities that are unchecked. I would be sincerely grateful for any suggestions. |
#2
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
Macro to delete text associated with any unchecked check boxes
If you put the cities in text form fields (set the city name as the default
text and uncheck the box 'fill in enabled') the following macro run on exit from the last check box field assumes three cities, with three check boxes Check1 to Check3 and three associated text fields Text1 to Text3. When the macro runs, each check box value is read and if the box is unchecked, both it and its associated text box are formatted as hidden, so effectively disappear from the document. http://www.gmayor.com/installing_macro.htm Sub ClearCities() Dim oFld As FormFields Set oFld = ActiveDocument.FormFields Dim bProtected As Boolean 'Unprotect the file If ActiveDocument.ProtectionType wdNoProtection Then bProtected = True ActiveDocument.Unprotect Password:="" End If 'Process each set of fields If oFld("Check1").CheckBox.Value = False Then oFld("Text1").Select Selection.Font.Hidden = True oFld("Check1").Select Selection.Font.Hidden = True End If If oFld("Check2").CheckBox.Value = False Then oFld("Text2").Select Selection.Font.Hidden = True oFld("Check2").Select Selection.Font.Hidden = True End If If oFld("Check3").CheckBox.Value = False Then oFld("Text3").Select Selection.Font.Hidden = True oFld("Check3").Select Selection.Font.Hidden = True End If 'Reprotect the document. If bProtected = True Then ActiveDocument.Protect _ Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="" End If End Sub The following macro will reset the fields to make them visible again Sub ResetFormFields() Dim bProtected As Boolean Dim oFld As FormFields Dim i As Long Set oFld = ActiveDocument.FormFields 'Unprotect the file If ActiveDocument.ProtectionType wdNoProtection Then bProtected = True ActiveDocument.Unprotect Password:="" End If 'Format all fields as not hidden For i = 1 To oFld.Count oFld(i).Select Selection.Font.Hidden = False Next 'Reprotect the document. If bProtected = True Then ActiveDocument.Protect _ Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="" End If End Sub -- Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org MIrving wrote: I have created a document with 10 paragraphs/lines. Each line has a check box form field and a city's name next to it. I would like to create a macro that can be run that deletes any cities that are unchecked. I would be sincerely grateful for any suggestions. |
#3
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
Macro to delete text associated with any unchecked check boxes
Thank you Graham. Is there any way of making it work without the bookmarks
(as the document automation program we use in conjuction with Word strips a merged document of all bookmarks)? "Graham Mayor" wrote: If you put the cities in text form fields (set the city name as the default text and uncheck the box 'fill in enabled') the following macro run on exit from the last check box field assumes three cities, with three check boxes Check1 to Check3 and three associated text fields Text1 to Text3. When the macro runs, each check box value is read and if the box is unchecked, both it and its associated text box are formatted as hidden, so effectively disappear from the document. http://www.gmayor.com/installing_macro.htm Sub ClearCities() Dim oFld As FormFields Set oFld = ActiveDocument.FormFields Dim bProtected As Boolean 'Unprotect the file If ActiveDocument.ProtectionType wdNoProtection Then bProtected = True ActiveDocument.Unprotect Password:="" End If 'Process each set of fields If oFld("Check1").CheckBox.Value = False Then oFld("Text1").Select Selection.Font.Hidden = True oFld("Check1").Select Selection.Font.Hidden = True End If If oFld("Check2").CheckBox.Value = False Then oFld("Text2").Select Selection.Font.Hidden = True oFld("Check2").Select Selection.Font.Hidden = True End If If oFld("Check3").CheckBox.Value = False Then oFld("Text3").Select Selection.Font.Hidden = True oFld("Check3").Select Selection.Font.Hidden = True End If 'Reprotect the document. If bProtected = True Then ActiveDocument.Protect _ Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="" End If End Sub The following macro will reset the fields to make them visible again Sub ResetFormFields() Dim bProtected As Boolean Dim oFld As FormFields Dim i As Long Set oFld = ActiveDocument.FormFields 'Unprotect the file If ActiveDocument.ProtectionType wdNoProtection Then bProtected = True ActiveDocument.Unprotect Password:="" End If 'Format all fields as not hidden For i = 1 To oFld.Count oFld(i).Select Selection.Font.Hidden = False Next 'Reprotect the document. If bProtected = True Then ActiveDocument.Protect _ Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="" End If End Sub -- Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org MIrving wrote: I have created a document with 10 paragraphs/lines. Each line has a check box form field and a city's name next to it. I would like to create a macro that can be run that deletes any cities that are unchecked. I would be sincerely grateful for any suggestions. |
#4
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
Macro to delete text associated with any unchecked check boxes
For a merge I would have been more inclined simply to insert the data that
was required rather than insert it then attempt to remove it. -- Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org MIrving wrote: Thank you Graham. Is there any way of making it work without the bookmarks (as the document automation program we use in conjuction with Word strips a merged document of all bookmarks)? "Graham Mayor" wrote: If you put the cities in text form fields (set the city name as the default text and uncheck the box 'fill in enabled') the following macro run on exit from the last check box field assumes three cities, with three check boxes Check1 to Check3 and three associated text fields Text1 to Text3. When the macro runs, each check box value is read and if the box is unchecked, both it and its associated text box are formatted as hidden, so effectively disappear from the document. http://www.gmayor.com/installing_macro.htm Sub ClearCities() Dim oFld As FormFields Set oFld = ActiveDocument.FormFields Dim bProtected As Boolean 'Unprotect the file If ActiveDocument.ProtectionType wdNoProtection Then bProtected = True ActiveDocument.Unprotect Password:="" End If 'Process each set of fields If oFld("Check1").CheckBox.Value = False Then oFld("Text1").Select Selection.Font.Hidden = True oFld("Check1").Select Selection.Font.Hidden = True End If If oFld("Check2").CheckBox.Value = False Then oFld("Text2").Select Selection.Font.Hidden = True oFld("Check2").Select Selection.Font.Hidden = True End If If oFld("Check3").CheckBox.Value = False Then oFld("Text3").Select Selection.Font.Hidden = True oFld("Check3").Select Selection.Font.Hidden = True End If 'Reprotect the document. If bProtected = True Then ActiveDocument.Protect _ Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="" End If End Sub The following macro will reset the fields to make them visible again Sub ResetFormFields() Dim bProtected As Boolean Dim oFld As FormFields Dim i As Long Set oFld = ActiveDocument.FormFields 'Unprotect the file If ActiveDocument.ProtectionType wdNoProtection Then bProtected = True ActiveDocument.Unprotect Password:="" End If 'Format all fields as not hidden For i = 1 To oFld.Count oFld(i).Select Selection.Font.Hidden = False Next 'Reprotect the document. If bProtected = True Then ActiveDocument.Protect _ Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="" End If End Sub -- Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org MIrving wrote: I have created a document with 10 paragraphs/lines. Each line has a check box form field and a city's name next to it. I would like to create a macro that can be run that deletes any cities that are unchecked. I would be sincerely grateful for any suggestions. |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Check Boxes and Text Fields | Microsoft Word Help | |||
Aligning check boxes and text boxes with regular text | Microsoft Word Help | |||
Aligning check boxes and text boxes with regular text | Microsoft Word Help | |||
delete text boxes from word document leaving text intact | Microsoft Word Help | |||
How do I align check boxes with the text that comes before it/ | Microsoft Word Help |