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

Summing a Table Column

Need to add a sum to a column of figures in a table? Word makes it relatively easy to provide the sum you need.

Discover More

Excluding Some Data from a Chart

Excel is a whiz at creating charts from your worksheet data. When the program tries to determine what should be included ...

Discover More

Ensuring Standardized Numbering

Want to make sure your paragraph numbering looks the same on different computer systems? It's a harder task in Word than ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (ribbon)

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

Copying Form Field Contents

Are you developing a form with Word? In some instances it is advantageous to copy whatever is entered in a form field to ...

Discover More

Safely Relocking Forms

In order to use a form in Word, it must be protected. This means that you cannot make any changes to the form, even if ...

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 two more than 7?

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.