Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General 2 / September 2003



Tip: Looking for answers? Try searching our database.

Using blobs in VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kim Jensen - 29 Sep 2003 10:22 GMT
I do apologize if this isn't the proper newsgroup, but I've found quite a
lot of potential news-group.

My problem is this: I'm trying to integrate our Oracle database with word.
The database contains amongst other type word documents, I want to provide
the end user a way to retrieve a document from the database edit it in word
and write it back to the database afterwards.

I've used oo4o for the job, but after retrieving the document in a variant
variable I'm stuck, I've tried to write the variant to disk in order to read
it later on, but the FileSystemObject and the WriteLine/Write methods
doesn't accept variant variables, and the Put statement writes record
information at the beginning of the file, causing the file to become
unreadable.

Does anyone know of a way to convert a variant variable to a word document?

Kind regards,

Kim Brandt Jensen
Murphy McCauley - 29 Sep 2003 13:57 GMT
...
> Does anyone know of a way to convert a variant variable to a word document?
...

It would help to know the Variant subtype -- it's probably a byte array
(good) or a string (bad).  "Debug.Print VarType(<Variable>)" should tell.

Murphy
www.ConstantThought.com
Kim Jensen - 29 Sep 2003 14:24 GMT
Hi Murphy,

it returns an integer 8209, in my documentation that number is not listed.
Do you know of a complete list of datatypes and their correlating vartype
values?

> ...
> > Does anyone know of a way to convert a variant variable to a word
[quoted text clipped - 6 lines]
> Murphy
> www.ConstantThought.com
Murphy McCauley - 29 Sep 2003 18:05 GMT
(Reply inline)

...
> it returns an integer 8209, in my documentation that number is not listed.
> Do you know of a complete list of datatypes and their correlating vartype
> values?

8209 is vbArray logically ORed against vbByte -- a byte array.

...
> > > Does anyone know of a way to convert a variant variable to a word
> > document?
...

Using my libraries (available on my site), you can do this in four lines of
code...

Require VarType(V) = (vbArray Or vbByte), "Operation only valid on Byte
Array Variants", vbeTypeMismatch
Dim F As New CFile
F.OpenFile "out.doc", fatGenericWrite, fcdCreateAlways
F.WriteData PeekLong(PeekLong(VarPtr(V) + 8) + 12), UBound(V) - LBound(V) +
1

(Where V is the Variant holding the blob)

Otherwise, you can use the following:

Private Const GENERIC_WRITE = &H40000000
Private Const CREATE_ALWAYS = 2
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA"
(ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal
dwShareMode As Long, lpSecurityAttributes As Any, ByVal
dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal
hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long)
As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long,
lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten
As Long, lpOverlapped As Any) As Long
Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory"
(Destination As Any, Source As Any, ByVal Length As Long)

Sub SaveVariantByteArray(FileName As String, Data As Variant)
   If VarType(Data) <> (vbArray Or vbByte) Then
       Err.Raise 5
   End If

   Dim hFile As Long
   hFile = CreateFile(FileName, GENERIC_WRITE, 0, ByVal 0&, CREATE_ALWAYS,
0, 0)
   If hFile = 0 Then
       Err.Raise 10101
   End If

   Dim TempL As Long
   MoveMemory TempL, ByVal VarPtr(Data) + 8, 4
   MoveMemory TempL, ByVal TempL + 12, 4

   WriteFile hFile, ByVal TempL, UBound(Data) - LBound(Data) + 1, 0, ByVal
0&

   CloseHandle hFile
End Sub

This should all be fairly clear, except the two MoveMemory() lines.  A quick
explaination of them:
A Variant is a VARIANT structure (see MSDN).
Offset 8 bytes into the structure is the real data that the Variant is
holding.  In the case of a byte array, it's the address of a SAFEARRAY
structure (see MSDN).
Offset 12 bytes into the SAFEARRAY structure is the address of the array's
data, which is what you want to write to disk.

Murphy
www.ConstantThought.com
Murphy McCauley - 29 Sep 2003 18:16 GMT
Oh.  I forgot to mention the simple, terrible, slow, but nonetheless working
method of simply iterating through the array writing each element
individually using the Put statement.  You could probably do similar with
the FSO using Chr$(), but at the risk of Byte->UnicodeChar->ASCIIChar
conversion problems (not to mention that it'd be even slower).

This is certainly a much simpler solution than the one I gave in the
previous post, but I would never use it.

Murphy
www.ConstantThought.com
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.