First, sorry for posting this here, but I couldn't find a VBScript forum on Daniweb (which I find surprising).
I'm working with binary data in several different projects. Below is an example of code that works to retrieve a static map image (PNG) from Google:
strFileURL = "http://maps.google.com/maps/api/staticmap?markers=2200+S+Western+Ave+Lisle+IL&zoom=14&size=400x400&sensor=false" Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP") Set objFSO = Createobject("Scripting.FileSystemObject") objXMLHTTP.open "GET", strFileURL, false objXMLHTTP.send() If objXMLHTTP.Status = 200 Then Set objADOStream = CreateObject("ADODB.Stream") objADOStream.Type = 1 'adTypeBinary objADOStream.Open objADOStream.Write objXMLHTTP.ResponseBody objADOStream.Position = 0 Set tempfolder = objFSO.GetSpecialFolder(2) tempname = tempfolder & "\" & objFSO.GetTempName If objFSO.Fileexists(tempname) Then objFSO.DeleteFile tempname objADOStream.SaveToFile tempname objADOStream.Close Set objADOStream = Nothing End if Set objXMLHTTP = Nothing Set objFSO = Nothing
That works fine. My next task is to retrieve binary data from an OLEobject field in Access. Modifying the code as follows:
dim conn set conn = CreateObject("ADODB.Connection") conn.Open "provider=microsoft.jet.oledb.4.0;data source=C:\db\db1.mdb" dim rs dim NewDoc set rs = CreateObject("ADODB.Recordset") rs.Open "Tom", conn rs.MoveFirst while not rs.EOF Set objADOStream = CreateObject("ADODB.Stream") objADOStream.Type = 1 'adTypeBinary objADOStream.Open() objADOStream.Position = 0 'Set the stream position to the start objADOStream.Write(rs.fields("ATTACHMENT_FILE").Value) Set tempfolder = objFSO.GetSpecialFolder(2) tempname = tempfolder & "\" & objFSO.GetTempName If objFSO.Fileexists(tempname) Then objFSO.DeleteFile tempname objADOStream.SaveToFile tempname objADOStream.Close Set objADOStream = Nothing rs.MoveNext wend
I get an error on the Write statement:
"Error 0 on line 21, column 3: ADODB.Stream: Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another."
I am not an Access pro, so my searching has been a bit haphazard. I've seen information that tells me I must strip the OLE Header... a lot of VBA or VB.NET code, but nothing which is specific to VBScript or explains why, OLE wrapper or not, I cannot at the very least save the binary object to disk.
I've also tried to first "size" the field value (.ActualSize) and instead of referencing the .Value property on the Write statement, use .GetChunk(size), with the same error.
So to word my question specifically: How does one write the contents of an Access OLEobject Field to disk using VBScript?
More specifically, I know that in this case the field contains a PDF, so information about the OLE Wrapper Access places around OLEobjects, and how to work with that wrapper (how big is it? What type is it? How to remove it?), again using VBScript, would be very much appreciated.