Capturing Paste event

A

Amir

Hi,

Is there any way to capture the Paste event to Word, so that each time a
user pastes a text into Word a macro will run?

If so, How can I check if the pasted data is text, and in case it's a text,
select the pasted text and do some changes in it using VBA code, e.g.
Selection.FormattedText.Font.Name = "Times New Roman" ?

I'm using Office2000.

Kind Regards,
Amir.
 
J

Jean-Guy Marcil

Amir was telling us:
Amir nous racontait que :
Hi,

Is there any way to capture the Paste event to Word, so that each
time a user pastes a text into Word a macro will run?

You can hijack the built-in paste methods that Word uses (Not that the sub
must be names as they are named here):

'_______________________________________
Sub EditPaste()

MsgBox "Trying to paste, are you?"

End Sub
'_______________________________________

'_______________________________________
Sub EditPasteSPecial()

MsgBox "Trying to paste special, are you?"

End Sub
'_______________________________________
If so, How can I check if the pasted data is text, and in case it's a

For this, you need to check the content of the clipboard:
See
http://word.mvps.org/FAQs/MacrosVBA/ManipulateClipboard.htm
for some information regarding basic requirements.

As for testing the clipboard content, try something like this:

'_______________________________________
Dim MyData As DataObject
Dim strClip As String

Set MyData = New DataObject
MyData.GetFromClipboard
If MyData.GetFormat(1) Then
'Thiere is text on the clipboard
strClip = MyData.GetText
Else
MsgBox "Sorry, no text in the clipboard"
End If
'_______________________________________
text, select the pasted text and do some changes in it using VBA
code, e.g. Selection.FormattedText.Font.Name = "Times New Roman" ?

For this last part, you are going to have to use your imagination to get the
user text from the clipboard to the document and manipulate it...
Use the Range object to insert the text string form the clipboard to the
document. Remember that a table will register as text ...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
[email protected]
Word MVP site: http://www.word.mvps.org
 
A

Amir

Hi Anne,

That's not exactly what I was looking for, but it gives me the idea of using
a different macro in order to paste that text instead of capturing the paste
event. It's less convinient but it should work.

Thank you.

Kind Regards,
Amir.
 
Top