Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.docmanagement
MIrving MIrving is offline
external usenet poster
 
Posts: 15
Default 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   Report Post  
Posted to microsoft.public.word.docmanagement
Graham Mayor Graham Mayor is offline
external usenet poster
 
Posts: 19,312
Default 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   Report Post  
Posted to microsoft.public.word.docmanagement
MIrving MIrving is offline
external usenet poster
 
Posts: 15
Default 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   Report Post  
Posted to microsoft.public.word.docmanagement
Graham Mayor Graham Mayor is offline
external usenet poster
 
Posts: 19,312
Default 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

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Check Boxes and Text Fields KrisRose Microsoft Word Help 1 April 5th 07 02:23 PM
Aligning check boxes and text boxes with regular text ssbell Microsoft Word Help 1 March 28th 07 02:12 AM
Aligning check boxes and text boxes with regular text Simon Jones [MSDL] Microsoft Word Help 0 March 28th 07 12:17 AM
delete text boxes from word document leaving text intact woody Microsoft Word Help 2 April 17th 06 03:30 PM
How do I align check boxes with the text that comes before it/ keithmichaelis Microsoft Word Help 6 May 9th 05 04:28 PM


All times are GMT +1. The time now is 07:11 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"