人力検索はてな
モバイル版を表示しています。PC版はこちら
i-mobile

ExcelのVBAで、AmazonのAPIを使って、
画像を取得してシート内に表示させようとしています。

シート上で表示できる画像と、
エラーが起きて表示できない画像があるのですが、
どうしたらよいでしょうか。

◆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")

●質問者: tetlis
●カテゴリ:コンピュータ インターネット
○ 状態 :終了
└ 回答数 : 1/1件

▽最新の回答へ

1 ● snow0214
●300ポイント ベストアンサー

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.人力検索はてなトップ
8.このページを友達に紹介
9.このページの先頭へ
対応機種一覧
お問い合わせ
ヘルプ/お知らせ
ログイン
無料ユーザー登録
はてなトップ