I need to create a code for formatting a file that I receive from Bloomberg, and has a line break at the end of each line. See below. (I have hundreds of paragraphs like this)

Italy’s debt is forecast to rise to 120 percent of gross
domestic product next year before declining. “I don’t care
about the debt-to-GDP ratio,” Baldassarri said in an interview
on Bloomberg Television in Rome today. “What goes on in the
market and must be underwritten by markets is not the debt-to-
GDP, but the billions of euros of actual debt.”

Basically what I need is for each line (starting from the second line) do a backspace and then insert a space ( " " )

So I guess the code in my loop should be like this:

Selection.TypeText Text:=" "

Does anyone know how I could create a loop to repeat that on each line that I select, or on an entire paragraph?

Please help! thanks in advance!!

6 Years
Discussion Span
Last Post by Ezzaral

Yes but the problem is that there is no specific string, the paragraph is just broken into different lines


The function I posted above replaces carriage return line feeds (CrLf) with a single space. The [yourStringHere] part should be whatever variable you use to hold that entire paragraph string.


I see. How can [yourstringhere] be the selected paragraph? Can I define a variable

Define a=Selection as string

Would it work? Sorry for my limited knowledge in Vba


In the Replace function, the second argument should be a string, however wdcrlf is not a string. what should I do?


This code fragment worked for me on the paragraph that you posted. I seldom use VBA for anything either, so my knowledge of it is pretty limited. I'm just hacking something together here

Dim str As String
Dim para As Paragraph
For Each para In ActiveDocument.Paragraphs
    str = str & Replace(para.Range.Text, vbCr, " ")
Next para
ActiveDocument.Range.Text = str
End Sub

Word was treating every line as a paragraph when I opened your text from a simple text file, so I had to concatenate them together to form the single string. Your file may or may not contain vbCR or vbCrLf ofr the line breaks. You'll have to determine that for yourself by examination.

Perhaps someone else more versed in VBA in Word will chime in with some further advice.


Thanks for your help but it's still not working for me. How do you concatenate the lines together?


In the for..each loop. Each paragraph is concatenated into 'str'. 'para.Range.Text' is the text of the current paragraph, in which I am replacing the carriage returns with a single space before I add it to the string.

Edited by Ezzaral: n/a


Well, I think that my file doesn't contain the vbCR. So it is not replacing anything. Where I am puzzled is that it worked for you on my paragraph because even now that I am copying it from this page your code doesn't help concatenating....

Is there something else you are adding at the begining of the macro?


I can't say for certain which character your file is actually using for the line break. A direct copy of your text above pasted into Word seems to have "vertical tab" (chr(11)) characters at the end of the lines.

Those can be replaced with the following loop

For i = 1 To ActiveDocument.Paragraphs.Count
    ActiveDocument.Paragraphs(i).Range.Text = Replace(ActiveDocument.Paragraphs(i).Range.Text, Chr(11), " ")
Next i

Those may not be the same characters coming in with your actual file though because copying and pasting into Word can vary depending on the copy source.


Thank you so much for your help! I tried and it worked perfectly, Chr(11) was the character on each line. This will help me save hours of work !

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.