Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word VBA: Combine multiple selected paragraphs into a single paragraph 2

Status
Not open for further replies.

CoxConsult

Programmer
Apr 1, 2011
4
US
Greetings,

I am copying and pasting text from a PDF into word. Word does not recognize the sentence structure in the PDF and pastes each line from the PDF as its own paragraph of text in the word document. Currently, I need to manually navigate to the end of the line delete the paragraph marker to join the parts of the text that belong together in a sentence.

For example, the pasted text of a short poem would contain two 'paragraphs' of text:


The cat fell (this is paragraph 1)
into the pot. (this is paragraph 2)


I would like to be able to select both paragraphs, run the macro and end up with the following in a single paragraph.

"The cat fell into the pot."

I am not new to VBA, but very new to using it with Word.

Has anyone seen this done before?

Thanks!

 


hi,

SELECT the portions you want to combines into a single paragraph.

Edit > Replace

^p

with ONE SPACE



Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Skip,

Thanks for the quick response. That was incredibly simpler than I thought it would be. Thank you much!

Lael
 
CoxConsult,

Welcome to Tek-Tips. One thing that would be good to read on getting the best answers to your questions, long term, and helping build a better source of data for everyone who has similar questions, I'd suggest looking here:
(Also found on the page just below where you would post a new question, or respond to an existing question/thread)

Here's the piece in short that I thought of highlighting for now:
On the other hand, members are expected to:

Search Google, Previous Posts, and FAQs Prior to Posting.
Write Intelligent Questions And Use Descriptive Subject Lines
[highlight]Thank Other Members For Help Received & Click the "Thank (member) For This Valuable Post!" Link[/highlight]
Help Other Members
Red Flag Offensive Posts
Be Professional

That one step goes a long way. So that when you're looking at a thread for an answer, you can often (though not always) find the correct/best answer by looking for posts where the asker (or whomever got help) gave the person with an answer a "pinky" or pink star.

It's a simple rating system. So the more "pinkys" a person gets, the more helpful they've been to others. So, I suppose it also helps show who at least seems to know the most on a certain topic. So when you ask a question, if you see a response from that pinkified user (yeah, I made that up), you can know there's a good chance that what they answer with is worth reading.

The more accurate we are with telling folks, "thank you", the more accurate that system will be.

But we hope to see you around more, asking questions, and answering other questions where you're able.
 
One last kink to work out...I generated the code below. Even though I select the relevant lines I want, the Find/Replace action runs through the whole document. Any ideas on how to restrict the action to the selected text?

Thanks in advance!


Sub ParagraphConsolidate()

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
Without me trying to put it all together, I searched and found this interesting page - lots of possibly useful snippets that you can use for learning and for modifying to fit your needs:

Under F, he has a couple things on Find... This one sounded like you may can modify to fit your "within selection" need:
Code:
[GREEN][B]'Find, Replace Hard Returns With Manual Line Breaks
'Within Selected Text:[/B][/GREEN]
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
   .Text = "^l"          'L not 1
   .Forward = False
   .Wrap = wdFindStop
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Find.ClearFormatting
With Selection.Find
   .Text = "^p"
   .Replacement.Text = "^l"       'L not 1
   .Forward = True
   .Wrap = wdFindStop
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.MoveRight Unit:=wdCharacter, Count:=1

I added in the apostrophes to set the "title" of that section as a comment, in case you tried to direct-paste it for testing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top