473,761 Members | 3,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4652
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 GetClipboardDat a() 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 GetClipboardDat a Lib "user32" (ByVal wFormat As Long)
As Long
Public Function ClipBoard_GetTe xt() As String
1000 debugStackPush mModuleName & ": ClipBoard_GetTe xt"
1001 On Error GoTo ClipBoard_GetTe xt_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 = GetClipboardDat a(CF_TEXT) 'Get
handle to global memory block that is referencing the text.
1030 If hClipMemory <> 0 Then
1040 hClipDat = GlobalLock(hCli pMemory) 'Lock
Clipboard memory so we can reference actual data.
1050 If hClipDat <> 0 Then
1051 L = GlobalSize(hCli pDat)
1052 clipText = Space$(L)
1053 returnValue = lstrcpy(clipTex t, hClipDat)
1054 returnValue = GlobalUnlock(hC lipMemory)
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_GetTe xt = clipText

ClipBoard_GetTe xt_xit:
debugStackPop
On Error Resume Next
Exit Function

ClipBoard_GetTe xt_err:
bugAlert True, "ReturnValu e='" & returnValue & "'."
Resume ClipBoard_GetTe xt_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 GetClipboardDat a() 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 GetClipboardDat a Lib "user32" (ByVal wFormat As Long)
As Long
Public Function ClipBoard_GetTe xt() As String
1000 debugStackPush mModuleName & ": ClipBoard_GetTe xt"
1001 On Error GoTo ClipBoard_GetTe xt_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 = GetClipboardDat a(CF_TEXT) 'Get
handle to global memory block that is referencing the text.
1030 If hClipMemory <> 0 Then
1040 hClipDat = GlobalLock(hCli pMemory) 'Lock
Clipboard memory so we can reference actual data.
1050 If hClipDat <> 0 Then
1051 L = GlobalSize(hCli pDat)
1052 clipText = Space$(L)
1053 returnValue = lstrcpy(clipTex t, hClipDat)
1054 returnValue = GlobalUnlock(hC lipMemory)
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_GetTe xt = clipText

ClipBoard_GetT ext_xit:
debugStackPop
On Error Resume Next
Exit Function

ClipBoard_GetT ext_err:
bugAlert True, "ReturnValu e='" & returnValue & "'."
Resume ClipBoard_GetTe xt_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 ("GetClipboardD ata").

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

strClipText = ClipBoard_GetTe xt()

Then I get the lines with:
strLines = Split(strClipTe xt, 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 ("GetClipboardD ata").

Nov 13 '05 #8
RE/
Then I get the lines with:
strLines = Split(strClipTe xt, 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_GetT ext() 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(strClipTe xt, 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_GetT ext() 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
8560
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 spreadsheet to place contents into a mySQL db. Thanx, Phil PS: can you use fopen() to read the contents and parse? The spreadsheet is on a remote site.
7
6032
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 the spreadsheet is located at: http://ratesheets.sollen.com/script/ratesheets/interfirst/ ratesheets/IFRS1_20030722_094227.xls At the heart of the problem is the sometimes varying layout of the spreadsheet. For example, the application
1
1561
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 side) * I understand how to do this
9
3920
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. There is no further explanation. What are the kinds of things that make this happen? Thanks from an obvious rookie. Gordon
5
8958
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 headers). I used ODBC in my VB.NET program to read that spreadsheet into a dataset, to make it easy to manipulate. The code I use to read it is as the bottom of this posting.
8
4871
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 into a document so I can distribute the tables, as is, as an electronic report (without the detailed data) So I export to rtf and xls, and I get an error that there is a too much information. I don't want all the data, just the summary table!
7
12073
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
2207
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 the right way of it there's an easier way to do it. Background/Platform: Windows XP Office Excel 2007 Visual Studio Tools for Office 2008 Document level customization VB.Net I have a spreadsheet that is used to project which site(s) a...
2
3211
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 : #!/usr/local/bin/perl use strict; use Spreadsheet::ParseExcel;
0
9336
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9948
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.