+ Reply to Thread
Results 1 to 7 of 7

Perform Find-and-replace on clipboard before pasting

  1. #1
    Registered User
    Join Date
    03-03-2014
    Location
    San Diego
    MS-Off Ver
    Office 2010
    Posts
    4

    Perform Find-and-replace on clipboard before pasting

    Hello all, here is my problem. Frequently I paste text from OCR'd sources. I wrote a macro to find and replace all paragraph marks within a selection (in MS Word) with spaces. So long as I don't copy more than 1 paragraph, the text wraps properly and is formatted as one paragraph, not a bunch of separate lines.

    However, currently I need to:
    1. copy
    2. paste
    3. highlight text
    4. run macro

    I would like to replace that with:
    1. copy
    2. run macro

    Step 2 would have a keyboard shortcut. The macro would be my existing macro, but would operate on the clipboard contents instead of text that I have pasted and subsequently highlighted in Word.

    Does anyone know how to perform a find and replace in VBA on the contents of the clipboard?

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Perform Find-and-replace on clipboard before pasting

    Nah, what you're doing now works. Just add the PASTE command to the front of the macro so it's the first thing it does before it continues on with the rest of the action.

    1) COPY
    2) Click on destination
    3) Run macro (hopefully by pressing a hotkey)

    Would that work?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    03-03-2014
    Location
    San Diego
    MS-Off Ver
    Office 2010
    Posts
    4

    Re: Perform Find-and-replace on clipboard before pasting

    That is a good idea, but won't work because if the context (that I didn't provide above). If I did that, the macro would run the Find and Replace on my whole document (bad), not just the text that I just pasted in (my goal). Another option other than what I originally asked would be to pasted, select the text that was just pasted, and then run the find and replace operation.

    Does that make sense?

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Perform Find-and-replace on clipboard before pasting

    No, sorry, I'm not following.

  5. #5
    Registered User
    Join Date
    03-03-2014
    Location
    San Diego
    MS-Off Ver
    Office 2010
    Posts
    4

    Re: Perform Find-and-replace on clipboard before pasting

    OK, I'll try to clarify. I have a document that is 50 pages or so. Throughout those 50 pages, there are headings, paragraphs, and maybe images. From a OCR'd document, maybe a PDF, or maybe from a web location, I copy some text. Because it was OCR'd, when pasted straight into Word, it looks like some number of one-line paragraphs. To fix that, I first highlight the text that I just pasted, and then I run my macro to FIND and REPLACE all of the carriage returns with spaces. Then the text looks like one paragraph.

    In summary:
    (1) paste the clipboard
    (2) highlight the text
    (3) run the macro (FIND & REPLACE).

    Key note: the macro is limited to operating ONLY on the text that I have highlighted.

    If I skip step (2), the Find and Replace would operate on all 50 pages of text, and I'd end up with a 30 page, single-paragraph, document. That's why I "select" the text first - to limit the scope of what is affected by the macro.

    I didn't think Word/VBA would "remember" what text was just pasted and re-select it, so I hoped to be able to run a macro on the clipboard contents only. Would something like this process flow work (which I don't know how to implement yet)?

    (1) save clipboard contents to a string or other variable type
    (2) run FIND & REPLACE on the variable
    (3) paste

  6. #6
    Registered User
    Join Date
    03-03-2014
    Location
    San Diego
    MS-Off Ver
    Office 2010
    Posts
    4

    Re: Perform Find-and-replace on clipboard before pasting

    Does anyone out there have any ideas on this? Thanks!

  7. #7
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Perform Find-and-replace on clipboard before pasting

    Perhaps you may try:
    Please Login or Register  to view this content.
    This will require reference to Microsoft Forms 2.0 Object library.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Pasting range from clipboard
    By mattmania in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-04-2013, 09:20 AM
  2. How do I insert a column without pasting from the clipboard?
    By Dave42 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-26-2012, 06:06 AM
  3. Cut & Pasting “Find & Replace” results
    By JRJ in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 06-28-2010, 09:33 AM
  4. Pasting in code and clipboard message
    By KarenH in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-02-2006, 02:30 PM
  5. VBA Bloomberg, Clipboard Pasting Problem
    By Teiresias in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-19-2005, 06:32 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1