Convert http get request image into Excel ActiveSheet.Pictures

830 views Asked by At

I have an image I get from request.responseBody, how can I pass it to excel without using a temp file in my computer storage?

Details:

  • I'm using a rest API with the image in the response body

  • I don't want to have a temp files in my desktop

  • Challenge is to convert the response from the API to Pictures.Insert or Shapes.AddPicture

I'm using something like this:

Sub GetPicAPI()
    Dim myUrl As String         ' path of image
    Dim myPicture As Picture    ' embedded image
    Dim MyImage As String       ' create string to receive image in text format
    Dim myFile As String
    Dim datim As String
    
    Set request = CreateObject("MSXML2.ServerXMLHTTP")
    datim = Format(CStr(Now), "yyyy_mm_dd_hh_mm_ss") 'datetime to generate file
    myFile = Application.DefaultFilePath & "\phototemp" & datim & ".jpeg"
    myUrl = "https://images.contoso.com/api/GetPic?pwd=1234=&id=1234res=low"
    request.Open "GET", myUrl, False         ' Where to get image
    request.send                             ' Send the request for the webpage.
    MyImage = StrConv(request.responseBody, vbUnicode)  ' Get the webpage response text into response variable.
    Open myFile For Output As #1  'open file to save image
    Print #1, MyImage             'write to file
    Close #1                      'close file
    Set myPicture = ActiveSheet.Pictures.Insert(myFile) 'put image into cell
End Sub
0

There are 0 answers