View Full Version : paste from clipboard into active window
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
 
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
shrivallabha
03-07-2012, 10:36 PM
Cross Posted:
http://www.mrexcel.com/forum/showthread.php?t=619607
Please read:
http://www.excelguru.ca/content.php?184
frank_m
03-08-2012, 01:28 AM
Edit: I noticed that I unnecessarily used two File System Object's: objFSO and filesys. 
I'll leave it to you to remove one so that you can make the code a little less long winded.
 
Why not just put the textbox value directly into a text file instead of to and from the clipboard?
 
What if there are several untitled notepads open?
 
If it were me I'd create the text file first, either manualy or with code.
 
I prefer using FSO, but there are other ways that require less code that you may prefer.
 
This FSO example creates a folder, if it does not exist, so you will need modify that to use an existing, or different path to suit your need.
 
You may also want to add commands to check if the file already exists, or if it's already open, otherwise you may accidently end up with multiple file version's.
 
Private Sub CommandButton3_Click()
Dim ObjFso As Object, objNewFile As Object
Dim filesys As Object, newfolder
Dim RetVal, MyValue
Set filesys = CreateObject("Scripting.FileSystemObject")
'change path to suit
If Not filesys.FolderExists("C:\TestFolder") Then
newfolder = filesys.CreateFolder("C:\TestFolder")
End If
Set filesys = Nothing
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'change path and file name to suit
Set objNewFile = ObjFso.CreateTextFile("C:\TestFolder\TestFile.txt", True)
Set ObjFso = Nothing
MyValue = Me.TextBox1.Value
objNewFile.writeline (MyValue)
objNewFile.Close
'change path and file name to suit
RetVal = Shell("C:\WINDOWS\notepad.exe C:\TestFolder\TestFile.txt", 3) 'the 3 is for maximized
End Sub
-Or- - If you really need to use the cliboard for a reason I can't think of, you can try this:
 
Private Sub CommandButton3_Click()
Dim ObjFso As Object, objNewFile As Object
Dim filesys As Object, newfolder
Dim objData As DataObject, strClipBoard As String
Dim RetVal
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 = objData.GetText
Set filesys = CreateObject("Scripting.FileSystemObject")
'change path to suit
If Not filesys.FolderExists("C:\TestFolder") Then
newfolder = filesys.CreateFolder("C:\TestFolder")
End If
Set filesys = Nothing
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'change path and file name to suit
Set objNewFile = ObjFso.CreateTextFile("C:\TestFolder\TestFile.txt", True) 
 
Set ObjFso = Nothing
objNewFile.writeline (strClipBoard)
objNewFile.Close
'change path and file name to suit
RetVal = Shell("C:\WINDOWS\notepad.exe C:\TestFolder\TestFile.txt", 3) 'the 3 is for maximized
End Sub
hi frank, thanks for your help and time on this. i will run this routine as well and see how i go. another way to do it has been shown here (http://www.mrexcel.com/forum/showpost.php?p=3072807&postcount=4).
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.
 
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.
 
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.mrexcel.com/forum/showthread.php?619607-paste-from-clipboard-into-active-window&p=3200529#post3200529
anyone have any ideas with this?
Maybe this suffices:
 
sub snb()
for j=1 to 10
c01= c01 & vbtab & me("Textbox" & j).Text
next
cells(1).value=c01
cells(1).copy
 
' activate the other program & paste
end sub
Maybe this suffices:
 
sub snb()
for j=1 to 10
c01= c01 & vbtab & me("Textbox" & j).Text
next
cells(1).value=c01
cells(1).copy
 
' activate the other program & paste
end sub
 
hi snb, thanks for the reply. I don't think that will work for me in what i am trying to do.
 
I have looked further into the clipboard way of doing it and this is where i am at atm.
 
I actually found out the reason why when I was copying multiple textbox values into AutoCAD wasnt working was because the windows clipboard cannot hold more than one text value at a time and each time a text value is copied into the clipboard it replaces its previous value. I found out that you can assign multiple text strings into the clipboard and can have more than one text value in there. I'll explore this option further tomorrow as well.
As above, i found out that if you copy more than one text string to the clipboard, the previous text string gets overwritten with the new one that is copied to it.
I came accross this tutorial...found here >>> http://www.cpearson.com/excel/Clipboard.aspx (http://www.cpearson.com/excel/Clipboard.aspx)
this is the code for copying a piece of text to the clipboard and then getting it from there.
Dim DataObj As New MSForms.DataObject
    Dim S As String
    S = "Hello World"
    DataObj.SetText S
    DataObj.PutInClipboard
DataObj.GetFromClipboard
    S = DataObj.GetText
this is my modified code that will focus on the cad window, paste the text that was retreived from the clipboard and then enter to the next attribute.
Dim DataObj As New MSForms.DataObject
    Dim S As String
    S = "Hello World"
    DataObj.SetText S
    DataObj.PutInClipboard
DataObj.GetFromClipboard
    S = DataObj.GetText
'Focus on autocad window
    AppActivate ("Enhanced Attribute Editor")
       
    'Paste command
    Application.SendKeys ("^v") 'Excel's method
    
    'Enter command
    Application.SendKeys ("~") 'Excel's method
that all works well, but then again, so did my code a couple of pages back that used individual buttons to copy each textbox.text.
now this is the code from that tutorial stating how to have more than 1 text string stored on the clipboard
Dim DataObj As New MSForms.DataObject
    Dim S1 As String
    Dim S2 As String
    S1 = "text string one"
    S2 = "text string two"
    With DataObj
        .SetText S1, "FormatId1"
        .PutInClipboard
        .SetText S2, "FormatId2"
        .PutInClipboard
        S1 = vbNullString
        S2 = vbNullString
        .GetFromClipboard
        S1 = .GetText("FormatId1")
        S2 = .GetText("FormatId2")
    End With
this is my modified code that will focus on the cad window, paste the text that was retreived from the clipboard and then enter to the next attribute for both text strings.
Dim DataObj As New MSForms.DataObject
    Dim S1 As String
    Dim s2 As String
    S1 = "Hello World"
    s2 = ", I'm Erik!"
    DataObj.SetText S1, "formatid1"
    DataObj.PutInClipboard
        
    DataObj.GetFromClipboard
    S1 = DataObj.GetText("formatid1")
              
    'Focus on autocad window
    AppActivate ("Enhanced Attribute Editor")
       
    'Paste command
    Application.SendKeys ("^v") 'Excel's method
    
    'Enter command
    Application.SendKeys ("~") 'Excel's method
        
    DataObj.SetText s2, "formatid2"
    DataObj.PutInClipboard
        
    DataObj.GetFromClipboard
    s2 = DataObj.GetText("formatid2")
  
    'Paste command
    Application.SendKeys ("^v") 'Excel's method
    'Enter command
    Application.SendKeys ("~") 'Excel's method
but this doesnt seem to work for me. Can you see an error as to why? I have been so close for so long but there is something i am not seeing that is preventing me from getting this to work.
You can try:
 
 
Sub snb()
    Cells(1).Value = "aaa~bbb~ccc~ddd~eee~fff"
    Cells(1).Copy
    'activate the other application
    Application.SendKeys ("^v")
End Sub
 
I thought most CAD program can import data from an ASCII file (.txt) ?
just an fyi, this has been solved.
 
you can see the end findings here
http://www.mrexcel.com/forum/showthread.php?619607-paste-from-clipboard-into-active-window&p=3214746&viewfull=1#post3214746
 
this is the end code i had. It;
*copies the text values from text boxes and combo boxes to the microsoft clipboard
*focus on the autocad attribute window
*pastes the first value to the first attribute pane
*scrolls down to the second and repeats the process 26 more times
*closes the autocad attribute window
 
Sub CommandButton41_Click()
Dim DataObj As MSForms.DataObject
   Dim S1 As String
   Dim s2 As String
   Dim s3 As String
   Dim s4 As String
   Dim s5 As String
   Dim s6 As String
   Dim s7 As String
   Dim s8 As String
   Dim s9 As String
   Dim s10 As String
   Dim s11 As String
   Dim s12 As String
   Dim s13 As String
   Dim s14 As String
   Dim s15 As String
   Dim s16 As String
   Dim s17 As String
   Dim s18 As String
   Dim s19 As String
   Dim s20 As String
   Dim s21 As String
   Dim s22 As String
   Dim s23 As String
   Dim s24 As String
   Dim s25 As String
   Dim s26 As String
   Dim s27 As String
          
   Set DataObj = New MSForms.DataObject
   
   S1 = TextBox1.Text
   s2 = TextBox2.Text
   s3 = TextBox3.Text
   s4 = TextBox4.Text
   s5 = TextBox5.Text
   s6 = TextBox6.Text
   s7 = TextBox7.Text
   s8 = ComboBox1.Text
   s9 = TextBox8.Text
   s10 = ComboBox2.Text
   s11 = TextBox9.Text
   s12 = ComboBox3.Text
   s13 = TextBox10.Text
   s14 = ComboBox4.Text
   s15 = TextBox11.Text
   s16 = TextBox12.Text
   s17 = TextBox13.Text
   s18 = TextBox14.Text
   s19 = TextBox15.Text
   s20 = TextBox16.Text
   s21 = TextBox17.Text
   s22 = TextBox18.Text
   s23 = TextBox19.Text
   s24 = TextBox20.Text
   s25 = TextBox21.Text
   s26 = TextBox22.Text
   s27 = TextBox23.Text
    
   'Focus on autocad window
   'AppActivate ("Enhanced Attribute Editor")
   AppActivate "Enhanced Attribute Editor", True
   'Shell "notepad", vbNormalFocus
   'AppActivate "Untitled - Notepad", True
   
'TEXTBOX1
    If TextBox1.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
   
        DataObj.SetText S1
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
'TEXTBOX2
    If TextBox2.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s2
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX3
    If TextBox3.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s3
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX4
    If TextBox4.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s4
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX5
    If TextBox5.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s5
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX6
    If TextBox6.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s6
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX7
    If TextBox7.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s7
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'COMBOBOX1
    If ComboBox1.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s8
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX8
    If TextBox8.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s9
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'COMBOBOX2
    If ComboBox2.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s10
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX9
    If TextBox9.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s11
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'COMBOBOX3
    If ComboBox3.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s12
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
'TEXTBOX10
    If TextBox10.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s13
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'COMBOBOX4
    If ComboBox4.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s14
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX11
    If TextBox11.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s15
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX12
    If TextBox12.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s16
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX13
    If TextBox13.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s17
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX14
    If TextBox14.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s18
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX15
    If TextBox15.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s19
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX16
    If TextBox16.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s20
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX17
    If TextBox17.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s21
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX18
    If TextBox18.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s22
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX19
    If TextBox19.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s23
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX20
    If TextBox20.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s24
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX21
    If TextBox21.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s25
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX22
    If TextBox22.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s26
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'TEXTBOX23
    If TextBox23.Text = "" Then
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    Else
        '- delay 1 second
        Application.Wait Now + TimeValue("00:00:01")
        
        DataObj.Clear
        DataObj.SetText s27
        DataObj.PutInClipboard
        'Paste command
        Application.SendKeys "^v", True 'Excel's method
        'Enter command
        Application.SendKeys "~", True 'Excel's method
        DoEvents
    End If
    
'CLOSE AUTOCAD ATTRIBUTE EDITOR WINDOW
    'TAB command
    Application.SendKeys "{TAB}", True 'Excel's method
    'Enter command
    Application.SendKeys "~", True 'Excel's method
    DoEvents
    
End Sub
Please read in your VBA book for beginners how to create a loop.
And study what is the essence of a variable (in which variable things can be put); you are treating all variables as if they are constants (non  variable).
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.