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 2003 Visual Basic 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.

There are also versions of these for Word 6.0 WordBasic. That page also discusses the line end conventions used by different text editor programs.

RemoveHardCRs

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

Attribute VB_Name = "RemoveHardCRs"

Public Sub MAIN()
Attribute MAIN.VB_Description = "Removes single hard carriage returns, but leaves double carriage returns to preserve paragraphs."
Attribute MAIN.VB_ProcData.VB_Invoke_Func = "TemplateProject.RemoveHardCRs.MAIN"
Dim CR$
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(WordBasic.[Selection$]()) <= 1 Then
    WordBasic.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
End If
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
WordBasic.EditReplace Find:="^w^p", Replace:="^p", Direction:=0, MatchCase:=0, WholeWord:=0, PatternMatch:=0, SoundsLike:=0, ReplaceAll:=1, Format:=0, Wrap:=0
WordBasic.Call "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
WordBasic.EditFind Find:=CR$, Direction:=0, MatchCase:=1, WholeWord:=0, PatternMatch:=0, SoundsLike:=0, Format:=0, Wrap:=0
If WordBasic.EditFindFound() Then
    WordBasic.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
End If

Rem ----------------------------------------------------------------------------
Rem CHANGE ALL DOUBLE-CRS TO CR$ SO THEY GET PRESERVED IN THE NEXT STEP
WordBasic.EditReplace Find:="^p^p", Replace:=CR$, Direction:=0, MatchCase:=0, WholeWord:=0, PatternMatch:=0, SoundsLike:=0, ReplaceAll:=1, Format:=0, Wrap:=0

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

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


Bye:

End Sub

AllCRCRLFtoCRLF

Changes soft (inside paragraph) carriage returns from Wordstar files to hard paragraph marks, in entire document.

Attribute VB_Name = "AllCRCRLFtoCRLF"

Public Sub MAIN()
Attribute MAIN.VB_Description = "Changes soft (inside paragrah) CRs from Wordstar files to hard CRs, in entire document."
Attribute MAIN.VB_ProcData.VB_Invoke_Func = "TemplateProject.AllCRCRLFtoCRLF.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

WordBasic.StartOfDocument
WordBasic.EditReplace Find:="^0013^0013^0010", Replace:="^p", Direction:=0, MatchCase:=0, WholeWord:=0, PatternMatch:=0, SoundsLike:=0, ReplaceAll:=1, 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.