Getting rid of carriage returns

A

Arnold

I am trying to write my first VBA application for Word
2000. What I'm attempting to do is to reformat a file I
imported from an Access report. The file is saved as an
..rtf, which I then saved as a .doc file. The problem is
that every line of text ends with a carriage return
character which screws up any attempt to make the Word
document look reasonable. I am trying to get rid of the
extraneous returns but have no idea of how to proceed.

The basic approach I want to try is to look for the Access
labels, which have a different color. When I encounter a
label I know that the last return was needed. Otherwise,
it should be deleted.

I have absolutely no idea as to how to proceed. Can anyone
help? Many thanks.
 
G

Guest

Sorry, I'm not very familiar with Access' rich text files, but what you
might try doing is use Word's macro recorder and record a Find/Replace
macro. This will let you choose the color of the character to delete.

You'll have to edit the script a bit. Your FindText will be Chr(13) and the
ReplaceText will be "".

Hope it helps.

-Brian
 
A

Art®

Here's a macro that I use, that should work on most versions of Word: The
macro removes unwanted paragraphs but retains paragraphs at the end of a
true paragraph. If it didn't it would reformat a document into one long
paragraph. Try is and see if it is helpful.

Sub StripParagraphs()
'
' StripParagraphs Macro
' Removes unwanted paragraphs.
'
WordBasic.EditReplace Find:="^p^p", Replace:="~", Direction:=0,
MatchCase:=0, WholeWord:=0, PatternMatch:=0, SoundsLike:=0, ReplaceAll:=1,
Format:=0, Wrap:=2

WordBasic.EditReplace Find:="^p", Replace:=" ", Direction:=0, MatchCase:=0,
WholeWord:=0, PatternMatch:=0, SoundsLike:=0, ReplaceAll:=1, Format:=0,
Wrap:=2

WordBasic.EditReplace Find:="~", Replace:="^p^p", Direction:=0,
MatchCase:=0, WholeWord:=0, PatternMatch:=0, SoundsLike:=0, ReplaceAll:=1,
Format:=0, Wrap:=2

End Sub

By the way, this macro has only three lines. Make sure that each of the
three lines of code are written as a single line in your macro editor.

Art
 
A

Arnold

Art,
Thanks for the help. Your macro works great on a document with a paragraph mark on each line and two for a blank line between paragraphs. Unfortunately, that's not my situation. I guess I didn't explain my problem clearly so I'll try again.

The document format consists of 'labels' that identify the variable data which follows. The labels are all in a 'dark red' color font. The text (variable data) has a carriage return at the end of each line including the last line of the text. The next label follows with more text.

Generating a macro with the find command is what I was trying but I can't figure out how to get it to recognize the ^p followed by a new font. If I could, I could use your logic to leave these ^p's alone.

I'd appreciate any other suggestions.

Thanks again
 
D

Doug Robbins - Word MVP

Hi Arnold,

Those following code will do what you want:

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="^p", Wrap:=wdFindStop, Forward:=True) =
True
Set myrange = ActiveDocument.Range
myrange.Start = Selection.Range.End + 1
If Not myrange.Words(1).Font.Color = wdColorDarkRed And
Len(myrange.Words(1)) > 1 Then
Selection.Range.Text = " "
End If
Loop
End With

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
Arnold said:
Art,
Thanks for the help. Your macro works great on a document with a
paragraph mark on each line and two for a blank line between paragraphs.
Unfortunately, that's not my situation. I guess I didn't explain my problem
clearly so I'll try again.
The document format consists of 'labels' that identify the variable data
which follows. The labels are all in a 'dark red' color font. The text
(variable data) has a carriage return at the end of each line including the
last line of the text. The next label follows with more text.
Generating a macro with the find command is what I was trying but I can't
figure out how to get it to recognize the ^p followed by a new font. If I
could, I could use your logic to leave these ^p's alone.
 
G

Guest

Doug,

Thanks so much. It works like a charm! Now all I have to
do is figure out why so I can write things like this.
Thanks again. You saved my life (or at least my sanity).

Arnold
-----Original Message-----
Hi Arnold,

Those following code will do what you want:

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="^p",
Wrap:=wdFindStop, Forward:=True) =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top