画像を取得してシート内に表示させようとしています。
シート上で表示できる画像と、
エラーが起きて表示できない画像があるのですが、
どうしたらよいでしょうか。
◆Amazon APIを使って取得した『銀河鉄道の夜 (280円文庫)』の表紙画像→
http://ecx.images-amazon.com/images/I/51aKVkh6SFL.jpg
ブラウザでは表示できるが、
ExcelのVBAで表示させようとするとエラーで表示されない。
ActiveSheet.Pictures.Insert ("http://ecx.images-amazon.com/images/I/51aKVkh6SFL.jpg")
◆Amazon APIを使って取得した『新編 銀河鉄道の夜』の表紙画像→
http://ecx.images-amazon.com/images/I/51VSTr9elML.jpg
ブラウザで表示できる上に、
ExcelのVBAでも表示できる。
ActiveSheet.Pictures.Insert ("http://ecx.images-amazon.com/images/I/51VSTr9elML.jpg")
amazonがリファラを見ているようなので、いったん画像ファイルをテンポラリにダウンロードしてから表示した方がいいようです。
テンポラリのファイル名は tmpfname に指定してください。
Private Declare Function URLDownloadToFile Lib "urlmon" Alias _ "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal _ szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long Sub GetImageFile(ImgName As String, SaveName As String) Dim SaveFileName As String, DownloadFile As String, Ret As Long If ImgName = "" Then Exit Sub SaveFileName = SaveName DownloadFile = ImgName Ret = URLDownloadToFile(0, DownloadFile, SaveFileName, 0, 0) If Ret <> 0 Then MsgBox "エラーが発生しました" End If End Sub Sub test() Dim imgfname As String, tmpfname As String imgfname = "http://ecx.images-amazon.com/images/I/51aKVkh6SFL.jpg" tmpfname = "c:\temp\tmp.jpg" 'テンポラリファイル名 Call GetImageFile(imgfname, tmpfname) ActiveSheet.Pictures.Insert (tmpfname) End Sub
コメント(0件)