Capturing Paste event

July 09th, 2005 - 03:17 am ET by Amir | Report spam
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.
email Follow the discussionReplies 5 repliesReplies Make a reply

Similar topics

Replies

#1 Anne Troy
July 09th, 2005 - 10:50 am ET | Report spam
For some reason, I don't think you can capture that event, but here's what
most people do:
http://www.vbaexpress.com/kb/getarticle.php?kb_id
*******************
~Anne Troy

www.OfficeArticles.com
www.MyExpertsOnline.com


"Amir" wrote in message
news:
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.




Replies Reply to this message
#2 Jean-Guy Marcil
July 09th, 2005 - 11:32 am ET | Report spam
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...pboard.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

Word MVP site: http://www.word.mvps.org
Replies Reply to this message
#3 Jean-Guy Marcil
July 09th, 2005 - 11:37 am ET | Report spam
Anne Troy was telling us:
Anne Troy nous racontait que :

For some reason, I don't think you can capture that event, but here's



Yes you can, see my reply in this same thread.

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP

Word MVP site: http://www.word.mvps.org
Replies Reply to this message
#4 Amir
July 09th, 2005 - 12:31 pm ET | Report spam
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.

"Anne Troy" wrote in message
news:7f5fd$42cfe44c$97c5108d$
For some reason, I don't think you can capture that event, but here's what
most people do:
http://www.vbaexpress.com/kb/getarticle.php?kb_id
*******************
~Anne Troy

www.OfficeArticles.com
www.MyExpertsOnline.com


"Amir" wrote in message
news:
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.








Replies Reply to this message
#5 Amir
July 09th, 2005 - 10:59 pm ET | Report spam
Thank you very much!

That's what I was looking for.

Regards!

"Jean-Guy Marcil" wrote in message
news:
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...pboard.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

Word MVP site: http://www.word.mvps.org





email Follow the discussion Replies Reply to this message
Help Create a new topicReplies Make a reply
Search Make your own search