#If VBA7 Then
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal ms As LongPtr)
#Else
Private Declare Sub Sleep Lib "kernel32" (ByVal ms As Long)
#End If
Sub otamart()
Dim objIE As InternetExplorer 'IEオブジェクトを準備
Set objIE = CreateObject("Internetexplorer.Application") '新しいIEオブジェクトを作成してセット
Dim CB As New DataObject
objIE.Visible = True 'IEを表示
Dim gazou As String
gazou = "画像パス"
Dim strUrl As String '次ページのURL
strUrl = "https://webapp.otamart.com/sell"
With CB 'クリックボードにコピー
.SetText gazou
.PutInClipboard
End With
objIE.navigate strUrl 'IEでURLを開く
Do While objIE.Busy = True Or objIE.readyState < READYSTATE_COMPLETE '読み込み待ち
DoEvents
Loop
Sleep (3000)
objIE.document.getElementsByName("picture00")(0).Click
Sleep (1000)
SendKeys "^v", True
SendKeys "%o", True
End Sub
VBAのSendkeyメソッドは安定しないので、あまり使わない方がいいです。
ここは、発想を転換し、指定のurlの画像を一気に保存していまう方法にしてはどうでしょうか?
パソコンに保存してしまえば、その後は、自由自在に扱えると思います。
Windows APIを多用してしまいますが、悪くない方法だと思います。
VBA
1Option Explicit
23Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias _
4 "URLDownloadToFileA" (ByVal pCaller As LongPtr, _
5 ByVal szURL As String, _
6 ByVal szFileName As String, _
7 ByVal dwReserved As LongPtr, _
8 ByVal lpfnCB As LongPtr) As LongPtr
910Declare PtrSafe Function DeleteUrlCacheEntry Lib "wininet" _
11 Alias "DeleteUrlCacheEntryA" (ByVal lpszUrlName As String) As LongPtr
1213Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal ms As LongPtr)
141516Sub ieCheck(objIE As InternetExplorer)
17 Dim timeOut As Date
1819 '完全にページが表示されるまで待機する
20 timeOut = Now + TimeSerial(0, 0, 20)
2122 Do While objIE.Busy = True Or objIE.readyState <> 4
23 DoEvents
24 Sleep 1
25 If Now > timeOut Then
26 objIE.Refresh
27 timeOut = Now + TimeSerial(0, 0, 20)
28 End If
29 Loop
3031 timeOut = Now + TimeSerial(0, 0, 20)
3233 Do While objIE.document.readyState <> "complete"
34 DoEvents
35 Sleep 1
36 If Now > timeOut Then
37 objIE.Refresh
38 timeOut = Now + TimeSerial(0, 0, 20)
39 End If
40 Loop
4142End Sub
4344Sub ieView(objIE As InternetExplorer, _
45 urlName As String, _
46 Optional viewFlg As Boolean = True, _
47 Optional ieTop As Integer = 0, _
48 Optional ieLeft As Integer = 0, _
49 Optional ieWidth As Integer = 600, _
50 Optional ieHeight As Integer = 800)
5152 'IE(InternetExplorer)のオブジェクトを作成する
53 Set objIE = CreateObject("InternetExplorer.Application")
5455 With objIE
5657 'IE(InternetExplorer)を表示・非表示
58 .Visible = viewFlg
5960 .Top = ieTop 'Y位置
61 .Left = ieLeft 'X位置
62 .Width = ieWidth '幅
63 .Height = ieHeight '高さ
6465 '指定したURLのページを表示する
66 .navigate urlName
6768 End With
6970 'IE(InternetExplorer)が完全表示されるまで待機
71 Call ieCheck(objIE)
7273End Sub
74757677Sub sample01()
7879 Dim objIE As InternetExplorer
80 Dim imgURL As String, fileName As String, savePath As String
81 Dim cacheDel As LongPtr, result As LongPtr
82 Dim i As Long
83 Dim strFolderText As String
84 Dim sw As Object, myPath As Object
858687 Set sw = CreateObject("Shell.Application")
88 Set myPath = sw.BrowseForFolder(&O0, "フォルダを選んでください", &H1 + &H10, "C:\")
89 If Not myPath Is Nothing Then
90 strFolderText = myPath.Items.Item.Path
91 Else
92 Set sw = Nothing
93 Set myPath = Nothing
94 Exit Sub
95 End If
96 Set sw = Nothing
97 Set myPath = Nothing
9899100 Call ieView(objIE, "https://transit.yahoo.co.jp/", False, 300, 300, 1800, 1200)
101102103 For i = 1 To objIE.document.images.Length
104105 '画像URL取得
106 imgURL = objIE.document.images(i - 1).src
107108 '画像ファイル名
109 fileName = Mid(imgURL, InStrRev(imgURL, "/") + 1)
110111 '画像保存先(+画像ファイル名)
112 savePath = strFolderText & "\" & fileName
113 Debug.Print savePath
114115 'キャッシュクリア
116 cacheDel = DeleteUrlCacheEntry(imgURL)
117118 '画像ダウンロード
119 result = URLDownloadToFile(0, imgURL, savePath, 0, 0)
120121 Next i
122123 objIE.Quit
124125 MsgBox "画像を指定のフォルダにダウンロードしました。"
126127End Sub
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。