PDA

View Full Version : Batch find and replace: Find using Clipboard content?



Cubajz
05-12-2017, 02:40 AM
Hi guys:),

I have this macro function for simple batch replacement using Graham Mayor :clap:awesome:clap: plugin. It works really well, however I was wondering: Would it be possible to change the macro, so that .Text = "test" 'word to find would be actualy "Clipboard Content", in other words that this macro would use clipboard content as text to find, something like: .Text = Clipboard content. I am newbie so sry for my uniteligent writings :doh:Thanks for any help in advance: pray2:, code is below:


Function Replace007(oDoc As Document) As Boolean
Dim oRng As Range
On Error GoTo err_handler
Set oRng = oDoc.Range
With oRng.Find
.Text = "test" 'word to find
.MatchWholeWord = True
.MatchCase = True
.Font.Italic = True
.Font.Color = wdColorRed
With .Replacement
.Text = "test" 'replacement word
.Font.Italic = False
.Font.Bold = True
.Font.Color = wdColorAutomatic
End With
.Execute Replace:=wdReplaceAll
End With
'Repeat as required e.g.,
'Set oRng = oDoc.Range
'...
Set oRng = oDoc.Range
With oRng.Find
.Text = "mouse" 'word to find
.MatchWholeWord = True
.MatchCase = True
.Font.Bold = True
.Font.Color = wdColorAutomatic
With .Replacement
.Text = "lion" 'replacement word
.Font.Italic = True
.Font.Bold = False
.Font.Color = wdColorBlue
End With
.Execute Replace:=wdReplaceAll
End With


Replace007 = True
lbl_Exit:
Exit Function
err_handler:
Replace007 = False
Resume lbl_Exit
End Function

gmaxey
05-12-2017, 04:07 AM
As Graham explicitly states, his awesome add-in is a collaborative effort by Graham and Greg.

Replace "test" with ^c

Edit,

Sorry I misread your question an thought you wanted to replace with the clipboard content.

gmayor
05-12-2017, 04:10 AM
To search for the text content of the clipboard you would need a function to get the text content of the clipboard and then use that string to search e.g.


Option Explicit

Function Replace007(oDoc As Document) As Boolean
Dim oRng As Range
Dim sFind As String
On Error GoTo err_handler
Set oRng = oDoc.Range
sFind = GetClipBoardText
If Not sFind = "" Then
With oRng.Find
.Text = sFind 'word to find
.MatchWholeWord = True
With .Replacement
.Text = "^&" 'the found text
.Font.Italic = True
.Font.Bold = False
.Font.Color = wdColorBlue
End With
.Execute Replace:=wdReplaceAll
End With
End If
Replace007 = True
lbl_exit:
Exit Function
err_handler:
Replace007 = False
Resume lbl_exit
End Function

Private Function GetClipBoardText() As String
Dim DataObj As DataObject
Set DataObj = New MsForms.DataObject
On Error GoTo lbl_exit
DataObj.GetFromClipboard
GetClipBoardText = DataObj.GetText(1)
Exit Function
lbl_exit:
If Err <> 0 Then
Err.Clear
GoTo lbl_exit
End If
End Function