Jump to content
msmith83100

paste from clipboard array into a program

Recommended Posts

Hello all!  I've been searching the help files and forums, maybe I've missed it but I'm looking for a place to start on pasting from an array I put onto the clipboard.  My code has the user select the cell(s) that they want put into the array, which copies just fine.  I've even got a message box to display the data to confirm.  But I want to be able to paste into ANY program from the cursor point while tabbing between elements...  Anyone able to help me with a decent starting point?  Here's what I have so far...

Local $vRange =0

Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\TESting.xlsx")


; Read the data of a cell range on specified workbook
;Local $aResult = _Excel_RangeRead($oWorkbook, 1, "A3:L30", 2)


MsgBox($MB_SYSTEMMODAL, "Info", "Select cells in Excel then click OK")
Local $vRange = $oExcel.Selection.Address
;Displays message box to select range of data selected
MsgBox($MB_SYSTEMMODAL, "Title", "Range value" & $vRange & "selected")
Local $aResult = _Excel_RangeRead($oWorkbook, 1, $vRange, 2)

;Displays messagebox, instructing user to go to window to start pasting data from array
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead Example 2", "Error reading from workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead Example 2", "Data successfully read." & @CRLF & "Please click 'OK' to paste the data from of cells " & $vRange &" of sheet 1.")

;get highlighted window and paste data from array with tab between elements


;Displays data inside selected cells
_ArrayDisplay($aResult, "Excel UDF: _Excel_RangeRead Example 2 - Cells A1:C1 of sheet 2")

Share this post


Link to post
Share on other sites

not what I should have said - there's a loop. But gives a subscript range dimension exceeded...

I see tons of fill an array, read an array examples... but not for pasting... Any help on elaborating?

 

Share this post


Link to post
Share on other sites

well it should also be Clipget no Clipput..

I also found this... but for some reason everything past MsgBox(xxx) gets put into message boxes... it's like it's not seeing the closing argument...

 

 

#include <Excel.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>

Local $vRange =0

Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\Test.xlsx")


; Read the data of a cell range on specified workbook
;Local $aResult = _Excel_RangeRead($oWorkbook, 1, "A3:L30", 2)


MsgBox($MB_SYSTEMMODAL, "Info", "Select cells in Excel then click OK")
Local $vRange = $oExcel.Selection.Address
;Displays message box to select range of data selected
MsgBox($MB_SYSTEMMODAL, "Title", "Range value" & $vRange & "selected")
Local $aResult = _Excel_RangeRead($oWorkbook, 1, $vRange, 2)

;Displays messagebox, instructing user to go to window to start pasting data from array
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead Example 2", "Error reading from workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead Example 2", "Data successfully read." & @CRLF & "Please click 'OK' to paste the data from of cells " & $vRange &" of sheet 1.")

;this area here also gets put into message boxes...

$content = ClipGet()
$array = StringSplit(StringStripCR($content), @LF)
If Not @error Then
    For $i = 1 To $array[0]
        MsgBox(0, '', $array[$i])
    Next
EndIf
;Displays data inside selected cells
_ArrayDisplay($aResult, "Excel UDF: _Excel_RangeRead Example 2 - Cells A1:C1 of sheet 2")

Share this post


Link to post
Share on other sites

@Nine I appreciate your help, but I've tried your fix, which it seems like you questioned when putting it out there.  You also had ClipPut which is to put into an array, I already have it in an array.  I'm trying to understand the printing (output) of the array.  I've looked for about 6 hours today just trying to find out how to print an array - hence the seeming of frustration.  Not being able to edit a post makes it look worse, cause you have to make a new post.  I've read help files and googled my brains out.

Share this post


Link to post
Share on other sites
2 hours ago, msmith83100 said:

well it should also be Clipget no Clipput

This is wrong. You stated that you want to paste text into another program, so you need to use Clipput to load the text onto the clipboard.

3 hours ago, msmith83100 said:

subscript dimension exceeded

This should have worked at posted by @Nine. Perhaps you didn't just copy/paste it into your script. <shrug>

Share this post


Link to post
Share on other sites
Posted (edited)

Nine solution is only for 1d array, you would need to use Ubound($aResult, 2) to access 2d array info (see my example array below).

Personally I would use _ArrayToString() something like:

#include <Array.au3>

Global $g_aArray[10][10]
For $i = 0 To Ubound($g_aArray) - 1
    For $j = 0 To Ubound($g_aArray, 2) - 1
        $g_aArray[$i][$j] = Chr(Random(65, 122, 1))
    Next
Next

_ArrayDisplay($g_aArray)

Global $g_sArray = _ArrayToString($g_aArray, @TAB)
ClipPut($g_sArray)
MsgBox(4096, "Example Table", "The following data has been captured to clipboard." & @CRLF & @CRLF & $g_sArray)

 

Edited by Subz
Forgot to post Ubound code.

Share this post


Link to post
Share on other sites
13 hours ago, Danp2 said:

This is wrong. You stated that you want to paste text into another program, so you need to use Clipput to load the text onto the clipboard.

This should have worked at posted by @Nine. Perhaps you didn't just copy/paste it into your script. <shrug>

Nope I copied and pasted.. Like I said, I already have it loading into the clipboard, thanks!

Share this post


Link to post
Share on other sites
13 hours ago, Subz said:

Nine solution is only for 1d array, you would need to use Ubound($aResult, 2) to access 2d array info (see my example array below).

Personally I would use _ArrayToString() something like:

#include <Array.au3>

Global $g_aArray[10][10]
For $i = 0 To Ubound($g_aArray) - 1
    For $j = 0 To Ubound($g_aArray, 2) - 1
        $g_aArray[$i][$j] = Chr(Random(65, 122, 1))
    Next
Next

_ArrayDisplay($g_aArray)

Global $g_sArray = _ArrayToString($g_aArray, @TAB)
ClipPut($g_sArray)
MsgBox(4096, "Example Table", "The following data has been captured to clipboard." & @CRLF & @CRLF & $g_sArray)

 

@Subz Thank you for your input... but that's a fixed array.  I thought about going that route, but as it sits; with this script I can select any amount of data and it will copy it into the clipboard.  I thought about doing a fixed array for say 50 entries, but that's not always needed.  If I only want to copy 10 items and paste, it's a little overkill and it might cause issues on pasting...  I think I might go the route of selecting data from one row to copy into the clipboard.  But basically If I can print a 1D (row) array of information I'm good.  I don't want it to paste to a MSGbox, I've already got that.

Share this post


Link to post
Share on other sites

Don't know what you mean by fixed array, the array is just an example of what could be returned by _Excel_RangeRead, the only part of the script above which is relevant to you, is everything below _ArrayDisplay($g_aArray).  You could also use _Excel_RangeCopyPaste, if you leave $vTargetRange to default, the data would be copied directly into the clipboard and will retain formatting etc..

Share this post


Link to post
Share on other sites
On 4/16/2019 at 6:02 PM, Nine said:

Ok you are getting a bit excessive.  I told you how to do it. So calm down, and read help file. And try to understand what you are doing...

Perhaps take your own advice and breathe a bit. New members cannot edit their posts, so after-thoughts are generally another post. The OP is simply trying to explain in detail what he is trying to do.

Share this post


Link to post
Share on other sites
17 hours ago, Nine said:

I breathe a lot, probably more than you do.  So you are the boss, do what you have to do...

@JLogan3o13 This a great way to make new people welcome, don't you think?  :) 

 

Share this post


Link to post
Share on other sites
17 hours ago, Nine said:

I breathe a lot, probably more than you do.  So you are the boss, do what you have to do...

Why this attitude suddenly, no need for that so please don't show that around here.

Thanks, Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

@JLogan3o13  Sounds great! 

I've got everything working except for the pasting of whats on the clipboard., which is an array. 

I can paste it to a message box just fine, but getting it to "paste" in general is the issue.  I've been using @Nine Ubound with ClipGet, but to no avail.

It seems the simplest and I guess should work, but I still get the subscript range dimension exceeded error...

As my code sits, I can go into Excel and copy any number of cells (1 to 3 rows 1 to 10 columns) and it will paste that data into a message box. 

I don't want a message box, I just want it to paste where ever the cursor is, tab, paste the next cell contents, tab, paste...etc.

 

Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\Test.xlsx")

MsgBox($MB_SYSTEMMODAL, "Info", "Select cells in Excel then click OK")
Local $vRange = $oExcel.Selection.Address
;Displays message box to select range of data selected
MsgBox($MB_SYSTEMMODAL, "Title", "Range value" & $vRange & "selected")
Local $aResult = _Excel_RangeRead($oWorkbook, 1, $vRange, 2)

;Displays messagebox, instructing user to go to window to start pasting data from array
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead Example 2", "Error reading from workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead Example 2", "Data successfully read." & @CRLF & "Please click 'OK' to paste the data from of cells " & $vRange & " of sheet 1.")

For $i = 0 to Ubound($aResult)-1

   ClipGet ($aResult[$i])
  Send ("^v")
  Sleep (150)
  Send ("{TAB}")
  Sleep (150)
Next

;Displays data inside selected cells
_ArrayDisplay($aResult)

Share this post


Link to post
Share on other sites

@Danp2  That's why I don't get it when I'm told to use ClipPut...  From the help file ClipPut writes text to the Clipboard...I already have it there  I need to paste it off the clipboard.  Didn't know Clipget wouldn't take parameters... it didn't have any in helpfile...I was just hoping it might.  thanks though!     Even if I use CLipPut, I still get the subscript range dimension exceeded error...   Next?  LOL 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...