Handling Returns in Form Data

Written by Allen Wyatt (last updated August 25, 2023)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


David created a form in Word. When he exports the data for a single form, he should get one long line of comma separated (CSV) information. Unfortunately, the content of some fields has returns in them. This means he gets a new line in the CSV every time this occurs. David wonders about the best way to handle this situation.

There are a couple of ways you could handle this situation. The first potential approach is to create a macro that is run whenever a form control is exited. This macro could be tied to the text form fields so that the contents of the fields are examined, and returns replaced with different characters, such as a space. Here's an example of such a macro:

Sub RemoveReturns()
    Dim sTemp as String

    sTemp = ActiveDocument.FormFields("Text1").Result
    sTemp = Replace(sTemp, vbCrLf, " ")
    ActiveDocument.FormFields("Text1").Result = sTemp
End Sub

All the macro does is grab the contents of the form field, replace the returns (vbCrLf) with spaces, and put the contents back into the form field. You can tie the macro to the form field in the following manner:

  1. Right-click on the field. Word displays a Context menu.
  2. Choose Properties from the Context menu. Word displays the Text Form Field Options dialog box. (See Figure 1.)
  3. Figure 1. The Text Form Field Options dialog box.

  4. In the Run Macro On section, use the Exit drop-down list to choose the macro you want run when the form field is exited.
  5. Click on OK.

Another approach is to get rid of the return characters after the CSV file is created (by exporting the form data). The CSV file is plain text, which means it can be loaded into a Word or a text editor. You could then use the Find and Replace features of the program to search for the returns and replace them with spaces. In doing so, however, you need to be careful of two things. First, you need to make sure you don't replace all returns—you want the ones at the end of each line in the CSV to remain as they are. Second, you need to make sure you save the CSV document back out without any formatting, meaning as a plain text file.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (5913) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Adding Leading Zeroes to ZIP Codes

Import a bunch of ZIP Codes into Excel, and you may be surprised that any leading zeroes disappear. Here's a handy little ...

Discover More

Using Multiple References to a Single Comment

Find yourself repeating the same comment over and over? Here's a couple of ways you can save some typing by simply ...

Discover More

Enabling Circular References by Default

Some formulas require the use of circular references in order to determine a result. If you want to make sure that ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Using Mandatory Form Fields

When using form fields to gather information from users of your documents, you may want to make sure that some of the ...

Discover More

Saving and Using a Form

After you have created your custom form, you will need to save it so that you can use it as often as needed. Word makes ...

Discover More

Working with Form Fields

You know you want to use form fields in your document (they are essential in creating forms, after all) but you need to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 4 + 5?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.