paste from clipboard into active window

epd

New Member
Joined
Feb 23, 2012
Messages
43
Hi,

I have been trying to work this problem out for ages but can't seem to find an answer.

this is what i have so far

Code:
Private Sub CommandButton3_Click()


Dim objData As DataObject
Dim strClipBoard As String
Set objData = New DataObject
'Clear clipboard
objData.SetText ""
objData.PutInClipboard
'Put text from textBox into clipboard
strClipBoard = Me.TextBox1.Value
objData.SetText strClipBoard
objData.PutInClipboard

'Get text from clipboard into a string variable
objData.GetFromClipboard
strClipBoard = ""
strClipBoard = objData.GetText

'activate window
AppActivate ("Untitled - Notepad")

= objData.GetText

End Sub

so when the button is clicked, textbox1 text is copied to the clip board, sets the focus to the notepad window and is then supposed to paste. what am i supposed to be having at the end? atm as you can see it is blank infront of '= objData.GetText'.

if i get rid of the appactivate and put textbox2.text infront of '= objData.GetText', then it pastes from the clipboard.

i want the contents from the clipboard to paste into the active window. any active window i say to be active.

thanks
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
i want the contents from the clipboard to paste into the active window. any active window i say to be active.

Not all windows accept pasting from the clipboard and those which do have different edit areas so you need to know which is the one to receive the data otherwise you will just be sending the data out randomly.
 
Upvote 0
Hello epd,

This macro uses the Windows API to locate Notepad and then paste the clipboard's text into it. Copy and paste this code into a separate VBA module in your workbook. Call the macro PasteIntoNotepad in your macro after you have copied the TextBox text to the clipboard.

Macro to Paste Clipboard Text into Notepad
Code:
' Written: March 08, 2012
' Author:  Leith Ross
' Summary: Paste Text From Clipboard into Notepad.

Private Const WM_CUT As Long = &H300
Private Const WM_COPY As Long = &H301
Private Const WM_PASTE As Long = &H302
Private Const WM_CLEAR As Long = &H303
Private Const WM_UNDO As Long = &H304

Private Const GW_CHILD As Long = 5

Private Declare Function FindWindow _
    Lib "user32.dll" Alias "FindWindowA" _
        (ByVal lpszClassName As String, ByVal lpszWindowTitle As String) _
    As Long

Private Declare Function GetWindow _
    Lib "user32.dll" _
        (ByVal hWnd As Long, ByVal wCmd As Long) _
    As Long

Private Declare Function SendMessage _
    Lib "user32.dll" Alias "SendMessageA" _
        (ByVal hWnd As Long, ByVal Msg As Long, _
         ByVal wParam As Long, ByRef lParam As Any) _
    As Long
    
    
Sub PasteToNotepad()

  ' This macro pastes the current contents of clipboard (text only) into Notepad where ever the cursor is.
  ' NOTE: This macro assumes there is only one instance of Notepad open.
  
    Dim Child As Long
    Dim hWnd As Long
    Dim RetVal As Long
    
        hWnd = FindWindow("Notepad", vbNullString)
        Child = GetWindow(hWnd, GW_CHILD)
        
        RetVal = SendMessage(Child, WM_PASTE, 0, ByVal 0)
        If RetVal = 0 Then MsgBox "Paste Operation Failed!", vbOKOnly + vbCrictical, "Paste Text to Notepad"
        
End Sub

Your Macro with Change Added
Code:
Private Sub CommandButton3_Click()


Dim objData As DataObject
Dim strClipBoard As String
Set objData = New DataObject
'Clear clipboard
objData.SetText ""
objData.PutInClipboard
'Put text from textBox into clipboard
strClipBoard = Me.TextBox1.Value
objData.SetText strClipBoard
objData.PutInClipboard

'Paste the TextBox text from the clipboard Text into Notepad
Call PasteIntoNotepad

End Sub
 
Last edited:
Upvote 0
Hi guys, thanks for all your replies.

Leith,

the below worked with notepad, though note that the 'Call PasteIntoNotepad' should be 'Call PastetoNotepad' as per the sub name, it worked then.

thankyou for your help. I will have a play around with it and see if i can get it to work with other applications.

Hello epd,

This macro uses the Windows API to locate Notepad and then paste the clipboard's text into it. Copy and paste this code into a separate VBA module in your workbook. Call the macro PasteIntoNotepad in your macro after you have copied the TextBox text to the clipboard.

Macro to Paste Clipboard Text into Notepad
Code:
' Written: March 08, 2012
' Author:  Leith Ross
' Summary: Paste Text From Clipboard into Notepad.
 
Private Const WM_CUT As Long = &H300
Private Const WM_COPY As Long = &H301
Private Const WM_PASTE As Long = &H302
Private Const WM_CLEAR As Long = &H303
Private Const WM_UNDO As Long = &H304
 
Private Const GW_CHILD As Long = 5
 
Private Declare Function FindWindow _
    Lib "user32.dll" Alias "FindWindowA" _
        (ByVal lpszClassName As String, ByVal lpszWindowTitle As String) _
    As Long
 
Private Declare Function GetWindow _
    Lib "user32.dll" _
        (ByVal hWnd As Long, ByVal wCmd As Long) _
    As Long
 
Private Declare Function SendMessage _
    Lib "user32.dll" Alias "SendMessageA" _
        (ByVal hWnd As Long, ByVal Msg As Long, _
         ByVal wParam As Long, ByRef lParam As Any) _
    As Long
 
 
Sub PasteToNotepad()
 
  ' This macro pastes the current contents of clipboard (text only) into Notepad where ever the cursor is.
  ' NOTE: This macro assumes there is only one instance of Notepad open.
 
    Dim Child As Long
    Dim hWnd As Long
    Dim RetVal As Long
 
        hWnd = FindWindow("Notepad", vbNullString)
        Child = GetWindow(hWnd, GW_CHILD)
 
        RetVal = SendMessage(Child, WM_PASTE, 0, ByVal 0)
        If RetVal = 0 Then MsgBox "Paste Operation Failed!", vbOKOnly + vbCrictical, "Paste Text to Notepad"
 
End Sub

Your Macro with Change Added
Code:
Private Sub CommandButton3_Click()
 
 
Dim objData As DataObject
Dim strClipBoard As String
Set objData = New DataObject
'Clear clipboard
objData.SetText ""
objData.PutInClipboard
'Put text from textBox into clipboard
strClipBoard = Me.TextBox1.Value
objData.SetText strClipBoard
objData.PutInClipboard
 
'Paste the TextBox text from the clipboard Text into Notepad
Call PasteIntoNotepad
 
End Sub
 
Upvote 0
Hello ebd,

If you want to paste into any active window then you should keep Jaafar's advice in mind. This is not as easy as it seems because you moving away form VBA and into the world of Windows programming.

The macro as is will paste only into Notepad. The main issue is finding the correct window handle to which the text will pasted. As Jaafar said, not all windows support pasting operations. The API is very powerful and you must pay attention to detail as there is no error handling. API errors can crash the system, corrupt open files and even corrupt the registry.
 
Upvote 0
Hi Leith,

the window i really want to paste into is autocad. the window name is Enhanced Attribute Editor. i replaced all the note pad references with the cad ones and it didnt work. i know cad can paste into autocad ebcause when i ran my original script without the paste and i manually pressed ctrl v, it pasted into the window. but for some reason it wont do it with vba.

i have tried to go down this route because i was not able to figure out how to run a script in excel and have it paste textbox values into attribute values, but that is another story.

Hello ebd,

If you want to paste into any active window then you should keep Jaafar's advice in mind. This is not as easy as it seems because you moving away form VBA and into the world of Windows programming.

The macro as is will paste only into Notepad. The main issue is finding the correct window handle to which the text will pasted. As Jaafar said, not all windows support pasting operations. The API is very powerful and you must pay attention to detail as there is no error handling. API errors can crash the system, corrupt open files and even corrupt the registry.
 
Upvote 0
Hello ebd,

If I had AutoCad then I could help you. My suspicion is AutoCad uses a Multiple Document Interface (MDI) window rather than a simple child window like Notepad. You may want to post your question with an AutoCad support group. Your chances of getting an answer quickly will be greatly improved.
 
Upvote 0
Hi guys, it totally slipped my mind to come back here and post what i had done for this.

ok, so i made progess on this. my current situation is this.

*i have a userform in excel.
*when it opens i have 3 (i have about 20 but to explain this, we will say 3 because it will make the code i paste here alot smaller) text boxes with a button next to each one.
*there is another button at the bottom of the userform called 'open title block template' which opens an autocad drawing for me
*i then double click on a block which contains attributes in the cad drawing and it opens up a window called 'enhanced attribute editor'
*now i go back to the excel user form where the 3 buttons that are next to 3 text boxes are
*each button when clicked copies the text located in the textbox it is next to onto the clipboard, sets focus to the 'enhanced attribute editor' window, pastes the contents of the clipboard using the send key paste, then goes down to the next attribute using the send key enter.
*afetr clicking on the first button, i then click on the second, third etc and i am copying each textbox text into each attribute in autocad.

here is the code for the above.

Code:
Private Sub CommandButton14_Click()
    Dim objData As DataObject
    Dim strClipBoard As String
    Set objData = New DataObject
    'Clears the clipboard
    objData.SetText ""
    objData.PutInClipboard
    'Puts the text from an textBox into the clipboard
    strClipBoard = Me.TextBox1.Value
    objData.SetText strClipBoard
    objData.PutInClipboard
    'Gets the text on the clipboard into a string variable
    objData.GetFromClipboard
    strClipBoard = ""
    strClipBoard = objData.GetText
    
    'Focus on autocad window
    AppActivate ("Enhanced Attribute Editor")
    
    'Paste command
    Application.SendKeys ("^v") 'Excel's method
   
    'Enter command and move down to next textbox in cad window
    Application.SendKeys ("~") 'Excel's method
                
End Sub
Private Sub CommandButton15_Click()
  
    Dim objData As DataObject
    Dim strClipBoard As String
    Set objData = New DataObject
    'Clears the clipboard
    objData.SetText ""
    objData.PutInClipboard
    'Puts the text from an textBox into the clipboard
    strClipBoard = Me.TextBox2.Value
    objData.SetText strClipBoard
    objData.PutInClipboard
    'Gets the text on the clipboard into a string variable
    objData.GetFromClipboard
    strClipBoard = ""
    strClipBoard = objData.GetText
    
    'Focus on autocad window
    AppActivate ("Enhanced Attribute Editor")
    
    'Paste command
    Application.SendKeys ("^v") 'Excel's method
   
    'Enter command
    Application.SendKeys ("~") 'Excel's method
    
    TextBox24.Text = Label3
       
End Sub
Private Sub CommandButton16_Click()
    Dim objData As DataObject
    Dim strClipBoard As String
    Set objData = New DataObject
    'Clears the clipboard
    objData.SetText ""
    objData.PutInClipboard
    'Puts the text from an textBox into the clipboard
    strClipBoard = Me.TextBox3.Value
    objData.SetText strClipBoard
    objData.PutInClipboard
    'Gets the text on the clipboard into a string variable
    objData.GetFromClipboard
    strClipBoard = ""
    strClipBoard = objData.GetText
    
    'Focus on autocad window
    AppActivate ("Enhanced Attribute Editor")
    
    'Paste command
    Application.SendKeys ("^v") 'Excel's method
   
    'Enter command
    Application.SendKeys ("~") 'Excel's method
    
    TextBox24.Text = Label4
       
End Sub

so i have been doing this for all my new drawings over the last few months which is fine but have now decided that i want to do it in one big hit. i actually looked at this soon after applying the abive code but i just couldnt get around the problem and ended up giving up.

but now i want to try and get it working again. so the problem was and still is that it all works and everything is dany, but tbh, i dont want to click the 20+ buttons just to copy the contents over. i want to be able to hit one button and it runs the command of all button clicks. so i came up with this.

Code:
Private Sub CommandButton14_Click()
    Dim objData As DataObject
    Dim strClipBoard As String
    Set objData = New DataObject
    'Clears the clipboard
    objData.SetText ""
    objData.PutInClipboard
    'Puts the text from an textBox into the clipboard
    strClipBoard = Me.TextBox1.Value
    objData.SetText strClipBoard
    objData.PutInClipboard
    'Gets the text on the clipboard into a string variable
    objData.GetFromClipboard
    strClipBoard = ""
    strClipBoard = objData.GetText
    
    'Focus on autocad window
    AppActivate ("Enhanced Attribute Editor")
    
    'Paste command
    Application.SendKeys ("^v") 'Excel's method
   
    'Enter command and move down to next textbox in cad window
    Application.SendKeys ("~") 'Excel's method
    
    
    
    
    Set objData = New DataObject
    'Clears the clipboard
    objData.SetText ""
    objData.PutInClipboard
    'Puts the text from an textBox into the clipboard
    strClipBoard = Me.TextBox2.Value
    objData.SetText strClipBoard
    objData.PutInClipboard
    'Gets the text on the clipboard into a string variable
    objData.GetFromClipboard
    strClipBoard = ""
    strClipBoard = objData.GetText
    
    'Focus on autocad window
    AppActivate ("Enhanced Attribute Editor")
    
    'Paste command
    Application.SendKeys ("^v") 'Excel's method
   
    'Enter command and move down to next textbox in cad window
    Application.SendKeys ("~") 'Excel's method
    
    
    
    
    
    Set objData = New DataObject
    'Clears the clipboard
    objData.SetText ""
    objData.PutInClipboard
    'Puts the text from an textBox into the clipboard
    strClipBoard = Me.TextBox3.Value
    objData.SetText strClipBoard
    objData.PutInClipboard
    'Gets the text on the clipboard into a string variable
    objData.GetFromClipboard
    strClipBoard = ""
    strClipBoard = objData.GetText
    
    'Focus on autocad window
    AppActivate ("Enhanced Attribute Editor")
    
    'Paste command
    Application.SendKeys ("^v") 'Excel's method
   
    'Enter command and move down to next textbox in cad window
    Application.SendKeys ("~") 'Excel's method
                
End Sub

now this works as in it goes through all 3 lots of code in one click but problem is it pastes the text from the third textbox into all three attributes in autocad as opposed to the first textbox text being pasted into the first attribute, the second in the second attribute and the third in the third. it always seems to paste the last textbox text into all attributes.

does anyone know what the problem is?

why would it work when i click each button, but when i run it in one routine, why doesnt it? i though it might be the fact that in the one routine it doesnt have the end sub command after each copy and paste. i then tried things like plication.CutCopyMode = False which didnt seem to help.

thankyou
epd

just a reminder that this is also posted here
http://www.vbaexpress.com/forum/showthread.php?p=271707#post271707
 
Upvote 0

Forum statistics

Threads
1,215,772
Messages
6,126,803
Members
449,337
Latest member
BBV123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top