PDA

View Full Version : Find and Replace for wdFieldFormCheckBox with text beside it



aqs
06-15-2020, 12:30 PM
Hello,

Beginner here! I'm trying to use a macro in Word to find a Legacy checkbox (wdFieldFormCheckBox) with particular text beside it, in order to delete both the checkbox and the text. I was using the following to find text and replace it with nothing, but I don't know how to include the checkbox in the "Find" function. Thanks for your help in advance!


Sub Test()


With Selection.Find


.ClearFormatting


.Replacement.ClearFormatting


.Text = "Protocol"


.Replacement.Text = ""


.Forward = True


.Wrap = wdFindContinue


.Format = False


.MatchCase = True


.MatchWholeWord = False


.MatchWildcards = False


.MatchSoundsLike = False


.MatchAllWordForms = False


.Execute Replace:=wdReplaceAll


End With

gmayor
06-15-2020, 07:59 PM
Assuming the check box and text are alone in a paragraph then with the form unprotected:


Sub Macro1()
Dim oFF As FormField
Dim orng As Range
For Each oFF In ActiveDocument.FormFields
If oFF.Type = wdFieldFormCheckBox Then
Set orng = oFF.Range.Paragraphs(1).Range
'orng.Select
If InStr(1, orng.Text, "Protocol") > 0 Then
orng.Text = ""
End If
End If
Next oFF
Set oFF = Nothing
Set orng = Nothing
End Sub

macropod
06-16-2020, 01:43 AM
Cross-posted at: https://stackoverflow.com/questions/62396438/find-and-replace-checkbox-and-text
Please read VBA Express' policy on Cross-Posting in Rule 3: http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_new_faq_item3