insert binary data to spreadsheet as picture

NewInExcelAndVBA

New Member
Joined
Nov 12, 2013
Messages
1
Hi,

I'm getting picture data through HTTP GET request (FileData = winHttpReq.ResponseBody). I want to embed the picture in the spreadsheet without saving it first. Currently I'm using the following code, which first saves the picture in the local storage and only then inserts the picture into excel. Please help me figure out how to do the same without storing the picture in the local storage:

Code:
[COLOR=#00008B][FONT=Consolas]Public[/FONT][/COLOR][COLOR=#00008B][FONT=Consolas]Sub[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] Test[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]()[/FONT][/COLOR]

<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;">[COLOR=#00008B]Dim[/COLOR] FileNum [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Long
[/COLOR][COLOR=#00008B]Dim[/COLOR] myURL [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]String
[/COLOR][COLOR=#00008B]Dim[/COLOR] FileData() [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Byte
[/COLOR][COLOR=#00008B]Dim[/COLOR] winHttpReq [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Object
[/COLOR][COLOR=#00008B]
Set[/COLOR] winHttpReq = CreateObject([COLOR=#800000]"WinHttp.WinHttpRequest.5.1"[/COLOR])
myURL = [COLOR=#800000]"..."
[/COLOR]winHttpReq.Open [COLOR=#800000]"GET"[/COLOR], myURL, [COLOR=#800000]False
[/COLOR]winHttpReq.Send

FileData = winHttpReq.ResponseBody

FileNum = FreeFileOpen [COLOR=#800000]"C:\Downloads\map.JPG"[/COLOR] [COLOR=#00008B]For[/COLOR] Binary Access Write [COLOR=#00008B]As[/COLOR] #FileNum    
    Put #FileNum, [COLOR=#800000]1[/COLOR], FileData
Close #FileNum

InsertPic
[COLOR=#00008B]
End[/COLOR] [COLOR=#00008B]Sub
[/COLOR][COLOR=#00008B]
Sub[/COLOR] InsertPic()
[COLOR=#00008B]
Dim[/COLOR] pic [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]String[/COLOR] 
[COLOR=#00008B]Dim[/COLOR] myPicture [COLOR=#00008B]As[/COLOR] Picture 
pic = [COLOR=#800000]"C:\Downloads\map.JPG"
[/COLOR][COLOR=#00008B]Set[/COLOR] myPicture = ActiveSheet.Pictures.Insert(pic)
[COLOR=#00008B]With[/COLOR] myPicture    
    .ShapeRange.LockAspectRatio = msoFalse
    .Top = ActiveSheet.Cells([COLOR=#800000]33[/COLOR], [COLOR=#800000]10[/COLOR]).Top    
    .Left = ActiveSheet.Cells([COLOR=#800000]33[/COLOR], [COLOR=#800000]10[/COLOR]).Left
[COLOR=#00008B]End[/COLOR] [COLOR=#00008B]With
[/COLOR]</code>[COLOR=#00008B]
End[/COLOR] [COLOR=#00008B]Sub[/COLOR]
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,216,045
Messages
6,128,480
Members
449,455
Latest member
jesski

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top