473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Parsing a copy buffer full of Excel spreadsheet rows?

I see this coming on a develpment effort that may materialize shortly.

These guys don't want to mess around with automated imports from text feeds BC
in the past they've had too many problems with same.

Instead, they (heavy spreadsheet users...) want to "Copy and Paste" financial
return percentages for various funds into the DB.

The source of these copies may be ascii text...but it's also likely to be an
Excel spreadsheet on occasion.

Text, I think, is no problem...I think I've done it before, just gotta dig out
the code.

For an Excel source, OTOH, it seems to me like I've got to figure out:

1) How to look in the copy buffer and determine what the source application was.

2) If the source application is Excel, parse the Excel-formatted data into
something that I can feed to a table.

Anybody been here?
--
PeteCresswell
Nov 13 '05 #1
9 4619
On Sat, 27 Nov 2004 01:32:24 GMT, "(Pete Cresswell)" <x@y.z> wrote:
I see this coming on a develpment effort that may materialize shortly.

These guys don't want to mess around with automated imports from text feeds BC
in the past they've had too many problems with same.

Instead, they (heavy spreadsheet users...) want to "Copy and Paste" financial
return percentages for various funds into the DB.

The source of these copies may be ascii text...but it's also likely to be an
Excel spreadsheet on occasion.

Text, I think, is no problem...I think I've done it before, just gotta dig out
the code.

For an Excel source, OTOH, it seems to me like I've got to figure out:

1) How to look in the copy buffer and determine what the source application was.

2) If the source application is Excel, parse the Excel-formatted data into
something that I can feed to a table.

Anybody been here?


Data in the clipboard can have more than one valid representation, and in the
case of Excel, it can also be interpreted as text by an application that only
understands text as a paste format. That means you can also read the
clipboard specifying that you expect to read text, and that will work. When
you do that, you get fields separated by tab characters, and rows separated by
line breaks. That format is pretty easy to parse.
Nov 13 '05 #2
RE/
That means you can also read the
clipboard specifying that you expect to read text, and that will work. When
you do that, you get fields separated by tab characters, and rows separated by
line breaks. That format is pretty easy to parse.


So the format that went into the clipboard becomes moot - all that matters is
what parm I specify when I parse it.

That's much better than I hoped.

Thanks.
--
PeteCresswell
Nov 13 '05 #3
On Sat, 27 Nov 2004 21:20:49 GMT, "(Pete Cresswell)" <x@y.z> wrote:
RE/
That means you can also read the
clipboard specifying that you expect to read text, and that will work. When
you do that, you get fields separated by tab characters, and rows separated by
line breaks. That format is pretty easy to parse.
So the format that went into the clipboard becomes moot - all that matters is
what parm I specify when I parse it.


Right - Excel tells the clipboard what formats it knows how to paste the
content as, and plan text is one of them. When you ask for the content as
plain text, Excel generates plain text.
That's much better than I hoped.

Thanks.


Your welcome :).
Nov 13 '05 #4
RE/
Right - Excel tells the clipboard what formats it knows how to paste the
content as, and plan text is one of them. When you ask for the content as
plain text, Excel generates plain text.
That's much better than I hoped.


Oops... nothing's simple, I guess.

I'm using the GetClipboardData() API call and feeding it a parm of CF_TEXT.

Problem is that it's returning space-delimited instead of tab-delimited text
when there are spreadsheet columns/rows in the copy buffer.

Tried CF_SYLK, but got some sort of error message.

Wrong API call?

Plagerized from Dev Ashish's site with error trapping added, some variables
renamed and comments reworded:
--------------------------------------------------------------------
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags&, ByVal
dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal
lpString2 As Any) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString
As String) As Long

Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As
Long

Private Declare Function OpenClipboard Lib "user32" (ByVal Hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long)
As Long
Public Function ClipBoard_GetText() As String
1000 debugStackPush mModuleName & ": ClipBoard_GetText"
1001 On Error GoTo ClipBoard_GetText_err

' PURPOSE: To retreive clipboard's contents as text
' RETURNS: Tab/CRLF delimited text file containing contents of clipboard

1002 Dim hClipMemory As Long
Dim hClipDat As Long
Dim clipText As String
Dim returnValue As Long
Dim L As Long

1010 If OpenClipboard(0&) <> 0 Then
1020 hClipMemory = GetClipboardData(CF_TEXT) 'Get
handle to global memory block that is referencing the text.
1030 If hClipMemory <> 0 Then
1040 hClipDat = GlobalLock(hClipMemory) 'Lock
Clipboard memory so we can reference actual data.
1050 If hClipDat <> 0 Then
1051 L = GlobalSize(hClipDat)
1052 clipText = Space$(L)
1053 returnValue = lstrcpy(clipText, hClipDat)
1054 returnValue = GlobalUnlock(hClipMemory)
1055 clipText = Left(clipText, InStr(1, clipText, Chr$(0), 0) - 1) '
Strip null terminating character.
1056 Else
1057 bugAlert True, "Could not lock memory to copy string from.
ReturnValue='" & returnValue & "'."
1059 End If
1100 End If
1200 CloseClipboard
1300 End If
1999 ClipBoard_GetText = clipText

ClipBoard_GetText_xit:
debugStackPop
On Error Resume Next
Exit Function

ClipBoard_GetText_err:
bugAlert True, "ReturnValue='" & returnValue & "'."
Resume ClipBoard_GetText_xit
End Function

--------------------------------------------------------------------
--
PeteCresswell
Nov 13 '05 #5
Sorry I was wrong about the plain text format. Did Dev's code do the trick?

The reason I thought the plain text format should be tabl delimited is that,
if you paste from Excel into Notepad, you get tab-delimited text in Notepad.
I couldn't imagine that Excel was doing anything other than asking for plain
text from the clipboard.

On Sun, 05 Dec 2004 16:41:02 GMT, "(Pete Cresswell)" <x@y.z> wrote:
RE/
Right - Excel tells the clipboard what formats it knows how to paste the
content as, and plan text is one of them. When you ask for the content as
plain text, Excel generates plain text.
That's much better than I hoped.


Oops... nothing's simple, I guess.

I'm using the GetClipboardData() API call and feeding it a parm of CF_TEXT.

Problem is that it's returning space-delimited instead of tab-delimited text
when there are spreadsheet columns/rows in the copy buffer.

Tried CF_SYLK, but got some sort of error message.

Wrong API call?

Plagerized from Dev Ashish's site with error trapping added, some variables
renamed and comments reworded:
--------------------------------------------------------------------
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags&, ByVal
dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal
lpString2 As Any) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString
As String) As Long

Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As
Long

Private Declare Function OpenClipboard Lib "user32" (ByVal Hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long)
As Long
Public Function ClipBoard_GetText() As String
1000 debugStackPush mModuleName & ": ClipBoard_GetText"
1001 On Error GoTo ClipBoard_GetText_err

' PURPOSE: To retreive clipboard's contents as text
' RETURNS: Tab/CRLF delimited text file containing contents of clipboard

1002 Dim hClipMemory As Long
Dim hClipDat As Long
Dim clipText As String
Dim returnValue As Long
Dim L As Long

1010 If OpenClipboard(0&) <> 0 Then
1020 hClipMemory = GetClipboardData(CF_TEXT) 'Get
handle to global memory block that is referencing the text.
1030 If hClipMemory <> 0 Then
1040 hClipDat = GlobalLock(hClipMemory) 'Lock
Clipboard memory so we can reference actual data.
1050 If hClipDat <> 0 Then
1051 L = GlobalSize(hClipDat)
1052 clipText = Space$(L)
1053 returnValue = lstrcpy(clipText, hClipDat)
1054 returnValue = GlobalUnlock(hClipMemory)
1055 clipText = Left(clipText, InStr(1, clipText, Chr$(0), 0) - 1) '
Strip null terminating character.
1056 Else
1057 bugAlert True, "Could not lock memory to copy string from.
ReturnValue='" & returnValue & "'."
1059 End If
1100 End If
1200 CloseClipboard
1300 End If
1999 ClipBoard_GetText = clipText

ClipBoard_GetText_xit:
debugStackPop
On Error Resume Next
Exit Function

ClipBoard_GetText_err:
bugAlert True, "ReturnValue='" & returnValue & "'."
Resume ClipBoard_GetText_xit
End Function

--------------------------------------------------------------------


Nov 13 '05 #6
RE/
Sorry I was wrong about the plain text format. Did Dev's code do the trick?
Not yet. I seem TB stuck on the lack of delimiter.
The reason I thought the plain text format should be tabl delimited is that,
if you paste from Excel into Notepad, you get tab-delimited text in Notepad.


I had exactly the same thought.

But NotePad if can do it, I should be able to do it... It's just a question of
figuring out how NotePad and so many other apps do it.

I was hoping somebody would tell me I'm using the wrong (outdated or
something...) API call ("GetClipboardData").

--
PeteCresswell
Nov 13 '05 #7
I get the clipboard contents with the ClipBoard_GetText() function
from http://www.mvps.org/access/api/api0049.htm

strClipText = ClipBoard_GetText()

Then I get the lines with:
strLines = Split(strClipText, vbCrLf, -1, vbTextCompare)

And the columns with:
strCols = Split(strLines(i), vbTab, -1, vbTextCompare)

And it works perfectly, whatever the source is (text or Excel cells
selection)

Hope this helps.

David.

"(Pete Cresswell)" <x@y.z> wrote in message news:<mh********************************@4ax.com>. ..
RE/
Sorry I was wrong about the plain text format. Did Dev's code do the trick?


Not yet. I seem TB stuck on the lack of delimiter.
The reason I thought the plain text format should be tabl delimited is that,
if you paste from Excel into Notepad, you get tab-delimited text in Notepad.


I had exactly the same thought.

But NotePad if can do it, I should be able to do it... It's just a question of
figuring out how NotePad and so many other apps do it.

I was hoping somebody would tell me I'm using the wrong (outdated or
something...) API call ("GetClipboardData").

Nov 13 '05 #8
RE/
Then I get the lines with:
strLines = Split(strClipText, vbCrLf, -1, vbTextCompare)

And the columns with:
strCols = Split(strLines(i), vbTab, -1, vbTextCompare)

And it works perfectly, whatever the source is (text or Excel cells
selection)

Hope this helps.


I feel a little foolish now.

Turns out that strClipText actually does contain the requisite tabs - as implied
by your code...it's just that I was invoking the function on the Immediate
window's command line as in ?ClipBoard_GetText() and somehow MS Access was
stripping out the tabs and replacing them with spaces....
--
PeteCresswell
Nov 13 '05 #9
On Mon, 06 Dec 2004 17:35:29 GMT, "(Pete Cresswell)" <x@y.z> wrote:
RE/
Then I get the lines with:
strLines = Split(strClipText, vbCrLf, -1, vbTextCompare)

And the columns with:
strCols = Split(strLines(i), vbTab, -1, vbTextCompare)

And it works perfectly, whatever the source is (text or Excel cells
selection)

Hope this helps.


I feel a little foolish now.

Turns out that strClipText actually does contain the requisite tabs - as implied
by your code...it's just that I was invoking the function on the Immediate
window's command line as in ?ClipBoard_GetText() and somehow MS Access was
stripping out the tabs and replacing them with spaces....


Yes, that's what the immediate window print does. It converts tabs into
spaces to place the text at a tab stop position. Try Print "a" & vbTab & "a".
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Phil Powell | last post by:
What would one best recommend to parse an existing Excel spreadsheet (was done in Excel 97 or 2000 not sure to be honest)? I am looking for the most practical way of parsing an existing...
7
by: Hugh McLaughlin | last post by:
Hello Everyone and thanks for your help in advance. I am working on an application that requires the parsing of an Excel spreadsheet that will be loaded into a SQL Server table. An example of...
1
by: JV | last post by:
Hi, I'm researching how I can : 1- dynamically generate an Excel spreadsheet by querying a database on an ASP page * I understand how to do this 2- open this spreadsheet by a user (client...
9
by: jillandgordon | last post by:
I am trying to import an excel file into Access 97. It looks perfectly all right but, every time I try to import it, I get to the lst step and am told that it was not imported due to an error. ...
5
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column...
8
by: Jerome Ranch | last post by:
Okay So I've got pivot tables setup in Access 2003. Only about 30K records in the current 2005 databases...the pivots summarize the info in a number of nice ways. I need to get the pivot tables...
7
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
0
by: dutsnekcirf | last post by:
Okay, I think this one is a really hard one for yous geniuses out there. I'm going to try my best to explain in as much detail as I can. And for the most part I just want to know if I'm doing this...
2
by: ravir81 | last post by:
Hi, I am working on excel reporting using Perl. I am facing problem with writing the header part only once for all the excels created using Perl. Here is the code : ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.