微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将 http 获取请求图像转换为 Excel ActiveSheet.Pictures

如何解决将 http 获取请求图像转换为 Excel ActiveSheet.Pictures

我有一张从 request.responseBody 获取的图像,如何在不使用计算机存储中的临时文件的情况下将其传递给 excel?

详情:

  • 我在响应正文中使用带有图像的 rest API

  • 我不想在我的桌面上有临时文件

  • 挑战是将来自 API 的响应转换为 Pictures.Insert 或 Shapes.AddPicture

我正在使用这样的东西:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。