Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cut and Paste w/Window focus problem

Status
Not open for further replies.

dprayner

Programmer
Oct 14, 2002
140
US
Hi I am using the VB Editor in MS Word and trying to cut and paste something into the body of the Document. The problem is I have can load the image into the clipboard, but the paste isn't working properly because the Form loses Focus and the Document gets the focus. I end up with the image inside the image, inside the image, inside the image, each time I run it. Part of the problem is the clipboard is not being cleared. Any suggestion? DAVE
 
Hi Dave,

Just some questions at the moment so I can understand what you're trying to do.

You say you're trying to paste into the body of the document but that's a bit vague - where exactly, do you have a Range or are you trying to use the Selection or what? And what are you cutting - where is it if not in the body of the document itself? And what sort of Form are you talking about? And how does that relate to the document and the cut and paste operation?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Hi Tony. I am trying to copy and paste a VB form into the body of a Word document. I am very close. I need the VBA code equivalent to typing ALT + Print Screen and CTRL + v. I figured out how to do this using SendKeys, but when I do it, it puts a copy of the VB Form (active window) and the image of the Word Document inside the body of the Word Document. When I use the ALT + Print Screen keys, only the VB FOrm gets paste in. I am confused at why the code version is achieving the desire results if it is invoke the code equivalent to the keystrokes? DAVE
 
And what about the Copy method of the UserForm object ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV. The copy method doesn't seem to work with Forms.

Thanks DAVE
 
Hi Dave,

I think you'll need APIs for this and I don't know how to do it I'm afraid.

Is it really something you need to do from VBA?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Mabye try using a DataObject. That way you may be able to get clipboard correctly. Also, the DataObject has a .Clear method.

Gerry
 
Hi People. Here is the code I have been using:

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_SNAPSHOT = &H2C
Private Const VK_MENU = &H12

Sub AltPrintScreen()
keybd_event VK_MENU, 0, 0, 0
keybd_event VK_SNAPSHOT, 0, 0, 0
keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0
keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
End Sub
Sub ClearClipboard()
Dim MyData As DataObject
Set MyData = New DataObject
MyData.SetText ""
MyData.PutInClipboard
End Sub


Private Sub cmdSubmit_Click()
Call ClearClipboard
'WordBasic.SendKeys ("%{prtsc}")
' ALT + Print Screen
WordBasic.SendKeys ("%{1068}")
UserForm1.Hide
'CTRL + v (Paste)
WordBasic.SendKeys ("^v")
Unload Me
End Sub

What this does is when the user clicks on the submit button, it pastes in a copy of the MS Word screen with the form inside in the body of the Word Document.

I heard that the Sendkeys method was unreliable and unpredictable, but I was hoping simulating these keystrokes would do the trick. Any suggestion? DAVE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top