#1
|
|||
|
|||
Why does this Macro take so long to execute?
I execute the following Macro after manually selecting a block of text that has been pasted into Word from a PDF to replace all the line breaks with spaces and thus make it a single, normal paragraph: Code:
Sub LinkLinesOfPDF_Text() Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = vbCr .Replacement.Text = " " .Forward = False '!!! .Wrap = wdFindStop '!!! .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute , , , , , , , , , , wdReplaceAll ' remove double spaces: Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = " " .Replacement.Text = " " .Forward = False .Wrap = wdFindStop .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll End Sub Is this delay unavoidable, or is there a more elegant, efficient way for me to replace all new lines with spaces in a given selection of text? Thank you so much in advance! |
#2
|
||||
|
||||
The delay may be due to some minor things in your code but may also be related to the format of what you have selected. I've done a tidy up on your code but it essentially does the same thing with less lines so may not be any quicker. I have a feeling that searching forward is faster than searching backwards but I could be wrong.
Code:
Sub MergeParagraph_Text() With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = "^p" 'same as vbCr but used by the find/replace dialog .Replacement.Text = " " .Forward = True 'search forwards instead of backwards .Wrap = wdFindStop 'stops replacing without asking if you want to do rest of doc .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute Replace:=wdReplaceAll ' remove double spaces: .Text = " " .Replacement.Text = " " .Execute Replace:=wdReplaceAll End With End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thank you so much for the advice. Unfortunately, your version still goes just as slow for me. It is strange, but I really think the slowness comes from the size of the word document I'm working in for some reason. I can paste the exact same text that the macro functions slowly on into a new word document and the find and replace action then works instantaneously, but in the big word document I've been working in, it takes several seconds to do the same thing. Not really sure why...
|
#4
|
||||
|
||||
Perhaps you should look at the Cleaning up Text Pasted from Websites, E-mails, PDFs etc. 'Sticky' thread at the top of the Word forum: https://www.msofficeforums.com/word/...s-e-mails.html
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Execute autocorrects? | KaiserD2 | Word | 6 | 04-03-2017 10:18 PM |
Simple Hide columns macro, when I execute it hides everything left of selection, please help | mcronin | Excel Programming | 2 | 08-31-2016 05:15 PM |
I need to convert shaded text into highlighted text on about 80 different long documents. VBA macro? | AustinBrister | Word VBA | 8 | 05-28-2015 02:42 PM |
Find and Execute | cksm4 | Word VBA | 1 | 10-22-2011 11:36 PM |
How do I call/execute an Excel macro from Access 2003 | KramerJ | Excel | 2 | 05-11-2009 11:33 AM |