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

Shortening ZIP Codes

US ZIP Codes can be of two varieties: five-digits or nine-digits. Here's how to convert longer ZIP Codes to the shorter ...

Discover More

Problem with Missing Context Menu Option

When you right-click a cell, does it seem that the Context menu is missing an item or two? Here's how to get those items ...

Discover More

Duplex by Default

Many printers these days have the capability to print on both sides of a piece of paper. You may want Word to use this ...

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)

Protecting Parts of a Document

Word doesn't require you to protect entire documents. Instead, you can protect different sections within a document, as ...

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

Spell Checking Forms

Word may be used to create protected forms that limit where the user may input data. Normally spell checking is disabled ...

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 6 + 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.