25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more...
Home   Projects   Up   Sitemap   Search   Blog   Forum+Chat   About Us   Privacy   Terms of Use   Feedback   FAQ   Images   Services   Payments   Humor   Music

Microsoft Word 6.0 WordBasic macros to remove or transform carriage returns in files

  • Manage carriage returns in a file.
  • Change WordStar soft carriage returns to Word paragraph marks.
  • Remove unwanted extra carriage returns from a file.
  • Easily modified to do other transformations.

Text editors, word processing programs, and even operating systems have different ideas about what should be used to mark the end of a line of text, known as a "line end", but they usually use a carriage return character (ASCII CR: 13 decimal or 0D hex), a line feed character (ASCII LF: 10 decimal or 0A hex), or some combination of them. 

When you import a file into a program, line ends are often automatically changed to match whatever convention that program uses.

In Word, a "paragraph mark" can be found (or replaced) using the search string "^p", meaning paragraph mark. It consists of a carriage return.

Notepad and WordPad end lines with <CR><LF>: an ASCII 13 character followed by an ASCII 10 character.

So, in the macro at the bottom of the page, you can see that it transforms old WordStar soft (wrappable) line ends CRCRLF to plain Word line ends: paragraph marks, CR.

There are also versions of these macros for Word 2003 Visual Basic.

RemoveHardCRs

Removes single hard carriage returns, but leaves double carriage returns to preserve paragraphs.

Sub MAIN
REM Copyright (C)1999 Steven Whitney.
REM Initially published by http://25yearsofprogramming.com.
REM Published under GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY.
REM REMOVES SINGLE HARD CARRIAGE RETURNS, 
REM BUT LEAVES DOUBLE CARRIAGE RETURNS TO PRESERVE PARAGRAPHS.
REM
REM IT MAY BE POSSIBLE TO USE AUTOFORMAT TO DO THIS.  TEST IT.
REM
REM IF YOUR SELECTION ENDS WITH A DOUBLE-CR, YOU SHOULD SELECT
REM THE DOUBLE-CR SO IT GETS PRESERVED AS A DOUBLE.
REM DON'T AVOID PROCESSING FINAL CRS BY ENDING YOUR SELECTION A LINE ABOVE.
REM (IF YOU DO, WHITESPACE ON LAST LINE WON'T GET REMOVED).
REM ----------------------------------------------------------------------------
REM YOU PROBABLY DON'T WANT TO DO THE WHOLE FILE.  IF YOU DO, SELECT ALL FIRST.
If Len(Selection$()) <= 1 Then
	MsgBox "You must select a block of text before calling this macro. If your selection ends with a double-CR, you should SELECT the double-cr so it gets preserved as a double.", "Cannot Continue", 48
	Goto bye
EndIf
REM ----------------------------------------------------------------------------
REM 11-24-99 REMOVE LEADING and trailing WHITESPACE FROM EACH LINE SO IT DOESN'T GET
REM PULLED INTO THE TEXT, CREATING GAPS.
REM unsure which should be done first
REM TrimTrailingWhitespace	'don't use: it's unfinished & cancels selection
REM next line does the job
EditReplace .Find = "^w^p", .Replace = "^p", .Direction = 0, .MatchCase = 0, .WholeWord = 0, .PatternMatch = 0, .SoundsLike = 0, .ReplaceAll, .Format = 0, .Wrap = 0
TrimLeadingWhitespace			'this correctly only processes the selection
REM ----------------------------------------------------------------------------
REM The string used for CR$ must not appear in the file.
REM if macro aborts, change it to something else here.
REM CR$ = "~~"		'for reference if CR$ is temporarily changed
Let CR$ = "~~"
REM ----------------------------------------------------------------------------
REM IF SELECTION ALREADY CONTAINS OUR SUBSTITUTE PARAGRAPH MARK, QUIT
EditFind .Find = CR$, .Direction = 0, .MatchCase = 1, .WholeWord = 0, .PatternMatch = 0, .SoundsLike = 0, .Format = 0, .Wrap = 0
If EditFindFound() Then
	MsgBox("This file already contains the string that this macro temporarily substitutes for double paragraph marks.  You must modify the macro for use with this file.", "Cannot Run Macro", 64)
	Goto bye
EndIf
REM ----------------------------------------------------------------------------
REM CHANGE ALL DOUBLE-CRS TO CR$ SO THEY GET PRESERVED IN THE NEXT STEP
EditReplace .Find = "^p^p", .Replace = CR$, .Direction = 0, .MatchCase = 0, .WholeWord = 0, .PatternMatch = 0, .SoundsLike = 0, .ReplaceAll, .Format = 0, .Wrap = 0

REM CHANGE ALL SINGLE CR TO SPACES SO PARAGRAPHS ARE ALLOWED TO WRAP.
EditReplace .Find = "^p", .Replace = " ", .Direction = 0, .MatchCase = 0, .WholeWord = 0, .PatternMatch = 0, .SoundsLike = 0, .ReplaceAll, .Format = 0, .Wrap = 0

REM PUT BACK THE DOUBLE-CRS
EditReplace .Find = CR$, .Replace = "^p^p", .Direction = 0, .MatchCase = 0, .WholeWord = 0, .PatternMatch = 0, .SoundsLike = 0, .ReplaceAll, .Format = 0, .Wrap = 0

bye:
End Sub

AllCRCRLFtoCRLF

Changes soft (inside paragraph) CRs from WordStar files to hard Word CRs, in entire document.

Sub MAIN
REM 10-28-00 
REM Copyright (C)2000 Steven Whitney.
REM Initially published by http://25yearsofprogramming.com.
REM Published under GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY.
REM Replaces all (interior) soft CRs from a WordStar Document mode document
REM with MSWord hard Paragraph Marks.  You must do this before running my
REM RemoveHardCRs macro, for it to properly remove them all.
REM This could use some enhancement.
REM SEE CLEANSTRING$() FOR INSIGHT ON HOW WORD HANDLES cr AND lf
StartOfDocument
EditReplace .Find = "^0013^0013^0010", .Replace = "^p", .Direction = 0, .MatchCase = 0, .WholeWord = 0, .PatternMatch = 0, .SoundsLike = 0, .ReplaceAll, .Format = 0, .Wrap = 0
End Sub

 

Valid HTML 4.01 Transitional Valid CSS
Yahoo! Search
Search the web Search this site
View content labeling at ICRA.