PDA

View Full Version : Solved: how can I delete carriage returns in text



dschmitt
06-08-2010, 06:46 AM
If I copy text from a PDF into Word I get all the carriage returns as they appear in the PDF file. I would like to remove these carriage returns so that the text ends up being in one paragraph.

A good way of doing it would be to highlight the text in Word and then execute a macro that removes the carriage returns.

Please anybody help with the macro. I have never written a macro for Word.

Tinbendr
06-08-2010, 09:46 AM
You don't need a macro. Use Find and Replace (http://word.mvps.org/FAQs/General/DeleteParaMarksAtEndOfLines.htm).

dschmitt
06-08-2010, 03:58 PM
I see. That is a way to do it. But this is still a time consuming way that involves several steps although much faster then doing it one CR at the time.

I think a macro would be faster. Especially in the form of an addin with a button in the menubar. It can't be that difficult to write.


BTW, I looked online for books or websites that teach about VBA for Word. I can't find anything. How does one learn VBA for Word?

Tinbendr
06-08-2010, 05:49 PM
Sub DeleteDupParagraphs()

With ActiveDocument.Range
With .Find
.Text = "^13^13"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
End With
End With

End Sub
Run it as many times as needed.


I looked online for books or websites that teach about VBA for Word. I can't find anything.I find that hard to believe. :think:

Look on this website for the links in the banner ads. I looked at a new book today following the link from here.

Look on Amazon, Ebay, Half.com... Google is your friend.

dschmitt
06-08-2010, 06:32 PM
John Wilson from the PowerPoint forum wrote the following script, which does the job.




Sub zapcr()

With Selection
.Text = Replace(.Text, vbCr, " ")
End With
End Sub


I attached a Word macro-enabled templete file that contains the macro and a menu button that executes it. Put it into the Word/Startup folder.


Tinbendr, thanks for your script. I just saw it. As a Word VBS scripter what do you think about John Wilsons script?
Thanks also for the hints regarding literature. I googled but had little luck with Word and PowerPoint VBA. There is lots on Excel VBA on the other hand.

fumei
06-09-2010, 10:44 AM
Sub NoCR()
ActiveDocument.Content = _
Replace(ActiveDocument.Content, vbCr, " ")
End SubReplaces all CR with a space. I must say that while this does precisely that, I have to wonder if this is precisely what you want. In any case, avoid using Selection.

What on earth did you google? "Word VBA Tutorial" gets LOTS! Also look at the Word MVP site.

dschmitt
06-09-2010, 03:56 PM
fumei, thanks for correcting the code I posted. Thank you also for telling me about Word MVP. There finally I found some useful information.

With Word VBA or PowerPoint VBA one gets a lot of hits in google but I found little information regarding VBA programming there. Word MVP is the best I found so far. I also now checked Amazon. There is a selection of books.

Thanks for your help fumei and Tinbendr.

fumei
06-10-2010, 09:00 AM
"but I found little information regarding VBA programming there. "

Really? Putting what I suggested into Google - "Word VBA tutorial" I can see (and have actually looked at):

"Using VBA in Word" which has a good link to "Word VBA Code samples" with plenty of references, and samples;

"Learn VBA Macro Coding with Word 2007 - A "From the Ground Up" Tutorial"

"Best Way - VBA Tutorial for Word"

and on and on.

Still, good that you found something at the MVP site. I also suggest that you study the actual Object Model:

http://msdn.microsoft.com/en-us/library/aa272078(office.11).aspx