Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-19-2020, 12:32 AM
alex100 alex100 is offline Replace double-paragraphs Windows 7 64bit Replace double-paragraphs Office 2016
Advanced Beginner
Replace double-paragraphs
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default Replace double-paragraphs

I have been using the script below to find and replace double-paragraph signs. It works ok, with an exception.



For some reason, it won't work with Quora.com pages. For example, please go to the address below and select & copy all the "Related Questions" text at the bottom of the page...

What makes you buy an Apple product? - Quora

Then paste this content to a Word document, and finally run this script...

Code:
With ActiveDocument.Content.Find
    .Execute FindText:="^p^p", ReplaceWith:="^p", Wrap:=wdFindStop, Replace:=wdReplaceAll
End With
You will see that although the pasted text contains lots of double-paragraphs, the script fails to replace them with a single paragraph mark, as it should.

Any idea how I could make it work, please?

Thank you!

Alex
Reply With Quote
  #2  
Old 07-19-2020, 03:27 AM
eduzs eduzs is offline Replace double-paragraphs Windows 10 Replace double-paragraphs Office 2019
Expert
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default

If you change FindStop to FindContinue ?
Reply With Quote
  #3  
Old 07-19-2020, 04:27 AM
alex100 alex100 is offline Replace double-paragraphs Windows 7 64bit Replace double-paragraphs Office 2016
Advanced Beginner
Replace double-paragraphs
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

Quote:
If you change FindStop to FindContinue ?
Unfortunately, it makes no difference at all.

Those 'related questions' at the bottom of the page are all links, and I believe this is the main reason for the script not working. As soon as I remove the links using the 'Remove Hyperlink' Word function, the script does start to work properly and replace the double paragraphs.

It's probably about the paragraphs that take the same property as the links next to them, and this will prevent the script to find them. It must be something like this, not sure exactly...

Any help would be appreciated.

Alex

Last edited by alex100; 07-19-2020 at 11:10 AM.
Reply With Quote
  #4  
Old 07-19-2020, 04:30 PM
Guessed's Avatar
Guessed Guessed is offline Replace double-paragraphs Windows 10 Replace double-paragraphs Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

That content is showing paragraph marks either end of a hyperlink. This means that there is 'something' in between the consecutive paragraph marks (ie a start or end hyperlink code).

If you replace all single paragraph marks with zz you see it hits them all. If you remove the hyperlinks you also see your original macro works as expected.

I suggest you rework your macro to remove paragraph marks from inside the Hyperlink ranges.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 07-20-2020, 04:35 AM
alex100 alex100 is offline Replace double-paragraphs Windows 7 64bit Replace double-paragraphs Office 2016
Advanced Beginner
Replace double-paragraphs
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

Quote:
That content is showing paragraph marks either end of a hyperlink. This means that there is 'something' in between the consecutive paragraph marks (ie a start or end hyperlink code).
Thank you for your explanation, Andrew!

Quote:
I suggest you rework your macro to remove paragraph marks from inside the Hyperlink ranges.
I did try to do that, but I'm afraid I do not have enough VBA experience for such a task.

Anyhow, if any of you already use a routine that is able to replace all double paragraph marks & carriage returns (including those inside hyperlink ranges, as explained above), it would be great if you could please share it here.

Thank you!

Alex
Reply With Quote
  #6  
Old 07-20-2020, 03:38 PM
Guessed's Avatar
Guessed Guessed is offline Replace double-paragraphs Windows 10 Replace double-paragraphs Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Try this
Code:
  Dim aHL As Hyperlink, sText As String
  For Each aHL In ActiveDocument.Hyperlinks
    sText = aHL.Range.Text
    sText = Replace(sText, Chr(13), "")
    aHL.TextToDisplay = sText
  Next aHL
  With ActiveDocument.Content.Find
    .Execute FindText:="^p^p", ReplaceWith:="^p", Wrap:=wdFindStop, Replace:=wdReplaceAll
  End With
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #7  
Old 07-21-2020, 04:14 AM
alex100 alex100 is offline Replace double-paragraphs Windows 7 64bit Replace double-paragraphs Office 2016
Advanced Beginner
Replace double-paragraphs
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

Wonderful, thank you very much! It works great!

I made a few changes. These are mostly personal preferences, but I'm pasting them here in case someone else might be interested in applying them. Otherwise, they can easily be removed.

Here they are, together with the code:

1) i need double-paragraphs to be replaced by a single paragraph, not entirely removed;

2) links are now blue and underlined;

3) during testing with various other content, I found some links that still had some large vertical space in between them, but that was only because of the 'ParagraphFormat.SpaceAfter' property, which is now set to 0;

4) I also found some links that had no anchor text at all - these will get deleted.

Code:
Dim aHL As Hyperlink, sText As String
For Each aHL In ActiveDocument.Hyperlinks
    sText = aHL.Range.Text
    sText = Replace(sText, Chr(13), "")
    aHL.TextToDisplay = sText + Chr(13)
    aHL.Range.Font.Color = vbBlue
    aHL.Range.Font.Underline = wdUnderlineSingle
    aHL.Range.ParagraphFormat.SpaceAfterAuto = False
    aHL.Range.ParagraphFormat.SpaceAfter = 0
    If sText = "" Then
        aHL.Delete
    End If
Next aHL
With ActiveDocument.Content.Find
    .Execute FindText:="^p^p", ReplaceWith:="^p", Wrap:=wdFindStop, Replace:=wdReplaceAll
End With
Alex
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace double-paragraphs Search and replace, with paragraphs and wildcards alex100 Word VBA 3 05-16-2020 08:43 AM
double spaced within paragraph 2 blank lines between paragraphs BigOldArt Word 1 08-24-2017 09:08 AM
Replace double-paragraphs Replacing till there's no more double paragraphs in the text eduzs Word VBA 25 07-01-2017 04:04 PM
Replace double-paragraphs Find and Replace multiple lines/paragraphs jcw Word 1 11-18-2011 11:47 AM
Replace double-paragraphs outlook double spacing paragraphs GWBDIRECT Outlook 3 04-06-2011 11:29 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:13 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft