Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 01-02-2025, 11:32 AM
robnun robnun is offline Paste from Clipboard, Find and Replace All, Then Copy Results Back to The Clipboard Windows 10 Paste from Clipboard, Find and Replace All, Then Copy Results Back to The Clipboard Office 2019
Novice
Paste from Clipboard, Find and Replace All, Then Copy Results Back to The Clipboard
 
Join Date: Dec 2020
Location: TX, USA
Posts: 8
robnun is on a distinguished road
Default Paste from Clipboard, Find and Replace All, Then Copy Results Back to The Clipboard

I am a macro novice and am trying to come up something that does the following in MS Word:



1) Paste the contents of the clipboard as plain text (into a Word document).

2) Find and replace all instances of <, >, and # with a dash.

3) Copy the resulting text back to the clipboard.

I found the following snippet online and it accomplishes steps 1-2 above, but I can't figure out how to copy the results back to the clipboard (Step 3). I am trying to use "ContentControl.Copy" but not sure where to add it into the existing code.

Thanks in advance for your help!

Code:
Sub FindReplaceAll()
Selection.Collapse Direction:=wdCollapseStart
Selection.PasteSpecial DataType:=wdPasteText
Dim StrFind As String, StrRepl As String
Dim FindItem As String, ReplaceItem As String
Dim i As Long
Dim aNote As Endnote
StrFind = "<,>#"
StrRepl = "-,-,-"
Set RngTxt = Selection.Range
For i = 0 To UBound(Split(StrFind, ","))
    FindItem = Split(StrFind, ",")(i)
    ReplaceItem = Split(StrRepl, ",")(i)
    Selection.HomeKey wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = FindItem
        .Replacement.Text = ReplaceItem
        .Format = False
        .MatchWholeWord = True
        .MatchAllWordForms = False
        .MatchWildcards = False
        .Execute Replace:=wdReplaceAll
    End With
    For Each aNote In ActiveDocument.Endnotes
        With aNote.Range
          .Find.Execute FindText:=FindItem, ReplaceWith:=ReplaceItem, Replace:=wdReplaceAll
        End With
    Next aNote
Next i
End Sub

Last edited by macropod; 01-02-2025 at 01:18 PM. Reason: Added code tags & corrected formatting
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
paste from clipboard to selected paragraphs yacov Word VBA 5 02-04-2024 01:41 PM
Paste from Clipboard, Find and Replace All, Then Copy Results Back to The Clipboard Replicate the copy/paste function, without involving the clipboard alex100 Word VBA 8 11-09-2020 08:19 AM
Need to copy specific formated text from Word and paste from clipboard into a console application Userx Word VBA 0 10-09-2020 05:58 AM
Error with Clipboard - Getting Spaces Out of Clipboard StephenRay Word VBA 14 09-27-2017 01:07 PM
Paste from Clipboard, Find and Replace All, Then Copy Results Back to The Clipboard Find & selected paras with highlighted text & copy to clipboard marceepoo Word VBA 1 09-14-2012 08:20 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:05 PM.


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