Share via

Use Search and Replace to remove fields

Anonymous
2012-04-13T14:15:56+00:00

Can someone help me with this problem?  I have a Word 2007 document with 388 occurrences of opening and closing square brackets in fields with the text between in italics eg: [name of company].   I don't want to have to go through the document using ALT + F1 to get to each individual field to delete then go back to search and replace the text. 

I would like to be able to remove the square brackets and the field they are in and change the text to normal in one sweep.  Is this possible using search and replace?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

6 answers

Sort by: Most helpful
  1. Charles Kenyon 167.7K Reputation points Volunteer Moderator
    2012-04-14T09:00:55+00:00

    I know it doesn't do what you asked to do, but if you have formfields in your document, is it protected for filling in forms? That is the proper way to use these fields, in which case you would move from field to field using the tab key.

    [FORMTEXT} indicates a formfield. The structure {FORMTEXT}Company Name{FORMTEXT} is just wrong. You shouldn't have anything using that structure. As Graham mentioned, macrobutton fields will often properly be used as prompt fields. That structure (with field codes displayed) would look like this:

    { MACROBUTTON NoMacro [Company Name] }

    With field codes not displayed, it would look like what you initially described:

    [Company Name]

    When you say square brackets are we talking about [ ] or about what :I call braces { }? I don't see any square brackets in what you provided with FORMTEXT showing.

    By the way, easier keyboard shortcuts for fields are F11 (next field) and SHIFT-F11 (previous field). These move from field to field in an unprotected document in the same way as ALT-F1 and SHIFT-ALT-F1 do.

    If you have some of these that you want to keep as fields, you might want to go through and mark the ones you want to keep with font color or other highlighting. Again, you'll need to change them from the {FORMTEXT}Company Name{FORMTEXT} structure, anyway. You'll want to change them to either a single FORMFIELD with default text in the field, or to macrobutton fields.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2012-04-14T05:58:56+00:00

    So what you are saying is that the document contains a series of form fields with text between and you want to remove some of the form fields but not others, leaving the text behind? Frankly I don't see how you will be able to do that without using a macro.

    What criteria determines which form fields are to go and which are to remain?

    If you have ONLY [ or ] in the form field and you know the words that you wish to process then the following macro will do that. Put the words in question in the list

    strWords = "Company Name|Address|Some other word(s)" each separated by '|'.

    Sub RemoveFormFields()

    Dim orng As Range

    Dim oFld As FormField

    Dim strWords As String

    Dim vFind As Variant

    Dim i As Long

    strWords = "Company Name|Address|Some other word(s)"

    vFind = Split(strWords, "|")

    For i = 0 To UBound(vFind)

        Set orng = ActiveDocument.Range

        With orng.Find

            Do While .Execute(FindText:=vFind(i)) = True

                orng.Start = orng.Start - 1

                orng.End = orng.End + 1

                For Each oFld In orng.FormFields

                    If InStr(1, oFld.Range.Text, "[") > 0 Or _

                       InStr(1, oFld.Range.Text, "]") > 0 Then

                        oFld.Delete

                    End If

                Next oFld

                orng.Font.Italic = False

                orng.Collapse wdCollapseEnd

            Loop

        End With

    Next i

    End Sub

    If that doesn't fix it, send me a sample of the document to the link on the home page of my web site.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2012-04-13T18:03:12+00:00

    Sorry, I forgot to say that there are other occurrences of square brackets with different text, these must remain in the document so I cannot just remove all fields using CTRL + Shift + F9.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2012-04-13T17:57:09+00:00

    They are form fields with the default text being the opening and closing square bracket at each side.

    Eg.

    **{FORMTEXT}**CompanyName{FORMTEXT}

    I would prefer not to use a macro but will if there is no other way.

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2012-04-13T14:50:07+00:00

    What sort of fields are we talking about? Press ALT+F9 to display the field and tell us EXACTLY what you see. My guess is that it is probably something like:

    { MacroButton NoMacro [Name of Company] }

    That being the case you can easily do what you want with a macro

    Dim oFld As Field

    For Each oFld In ActiveDocument.Fields

        If oFld.Type = wdFieldMacroButton Then

            oFld.Code.Text = Replace(oFld.Code.Text, "[", "")

            oFld.Code.Text = Replace(oFld.Code.Text, "]", "")

            oFld.Code.Font.Italic = True 'Set to false to remove the italics.

            oFld.Unlink

        End If

    Next oFld

    http://www.gmayor.com/installing_macro.htm

    Was this answer helpful?

    0 comments No comments