質問編集履歴

2

構文間違え

2015/04/19 07:37

投稿

ttt1212
ttt1212

スコア16

test CHANGED
File without changes
test CHANGED
@@ -184,7 +184,7 @@
184
184
 
185
185
  .Write xhr.responseBody
186
186
 
187
- .SaveToFile ActiveWorkbook.Path & "\vbatemp", adSaveCreateOverWrite
187
+ .SaveToFile Environ("TEMP") & "\vbatemp", adSaveCreateOverWrite
188
188
 
189
189
  .Close
190
190
 

1

現在のコードを記載

2015/04/19 07:37

投稿

ttt1212
ttt1212

スコア16

test CHANGED
File without changes
test CHANGED
@@ -63,3 +63,193 @@
63
63
 
64
64
 
65
65
  End Sub
66
+
67
+
68
+
69
+ ーーーーーーーー
70
+
71
+ 整理のため、今一度コードを現在のコードを記載します。
72
+
73
+ ※htsignさんより回答いただき、下記のように編集いたしました。
74
+
75
+ ```lang-VBAスプリクト
76
+
77
+ Option Explicit
78
+
79
+
80
+
81
+ Private Sub Main()
82
+
83
+ Call GoogleSearch(ActiveCell.Value2)
84
+
85
+ End Sub
86
+
87
+
88
+
89
+ Private Sub GoogleSearch(ByVal query As String)
90
+
91
+ Dim html As String
92
+
93
+ html = FetchHtml("https://www.google.co.jp/search?tbm=isch&q=" & query)
94
+
95
+
96
+
97
+ Dim nextUrl As String
98
+
99
+ nextUrl = FindFirstUrlFromGoogleImageSearch(html)
100
+
101
+
102
+
103
+ DownloadFileToTempDir nextUrl
104
+
105
+
106
+
107
+ AddPicture
108
+
109
+ End Sub
110
+
111
+
112
+
113
+ Private Function FetchHtml(ByVal url As String) As String
114
+
115
+ Dim xhr As Object
116
+
117
+ Set xhr = CreateObject("MSXML2.XMLHTTP")
118
+
119
+
120
+
121
+ xhr.Open "GET", url, True
122
+
123
+ xhr.send
124
+
125
+
126
+
127
+ Do Until xhr.readyState = 4
128
+
129
+ DoEvents
130
+
131
+ Loop
132
+
133
+
134
+
135
+ FetchHtml = xhr.responseText
136
+
137
+
138
+
139
+ Set xhr = Nothing
140
+
141
+ End Function
142
+
143
+
144
+
145
+ Private Sub DownloadFileToTempDir(ByVal url As String)
146
+
147
+ Const adTypeBinary = 1
148
+
149
+ Const adSaveCreateOverWrite = 2
150
+
151
+
152
+
153
+ Dim xhr As Object
154
+
155
+ Set xhr = CreateObject("MSXML2.XMLHTTP")
156
+
157
+
158
+
159
+ xhr.Open "GET", url, True
160
+
161
+ xhr.setRequestHeader "Pragma", "no-cache"
162
+
163
+ xhr.setRequestHeader "Cache-Control", "no-cache"
164
+
165
+ xhr.setRequestHeader "If-Modified-Since", "Thu, 01 Jun 1970 00:00:00 GMT"
166
+
167
+ xhr.send
168
+
169
+
170
+
171
+ Do Until xhr.readyState = 4
172
+
173
+ DoEvents
174
+
175
+ Loop
176
+
177
+
178
+
179
+ With CreateObject("ADODB.Stream")
180
+
181
+ .Type = adTypeBinary
182
+
183
+ .Open
184
+
185
+ .Write xhr.responseBody
186
+
187
+ .SaveToFile ActiveWorkbook.Path & "\vbatemp", adSaveCreateOverWrite
188
+
189
+ .Close
190
+
191
+ End With
192
+
193
+ End Sub
194
+
195
+
196
+
197
+ Private Function FindFirstUrlFromGoogleImageSearch(ByVal html As String) As String
198
+
199
+ Dim partOfHtml As String
200
+
201
+ Dim idx As Long
202
+
203
+
204
+
205
+ idx = InStr(html, "imgurl=")
206
+
207
+ partOfHtml = Mid(html, idx + 7)
208
+
209
+ idx = InStr(partOfHtml, "&")
210
+
211
+
212
+
213
+ FindFirstUrlFromGoogleImageSearch = Left(partOfHtml, idx - 1)
214
+
215
+ End Function
216
+
217
+
218
+
219
+ Private Sub AddPicture()
220
+
221
+ Dim shape As shape
222
+
223
+ Set shape = ActiveSheet.Shapes.AddPicture( _
224
+
225
+ Filename:=Environ("TEMP") & "\vbatemp", _
226
+
227
+ LinkToFile:=False, _
228
+
229
+ SaveWithDocument:=True, _
230
+
231
+ Left:=ActiveCell.Left + ActiveCell.Width, _
232
+
233
+ Top:=ActiveCell.Top, _
234
+
235
+ Width:=0, _
236
+
237
+ Height:=0)
238
+
239
+
240
+
241
+ shape.ScaleHeight 1, msoTrue
242
+
243
+ shape.ScaleWidth 1, msoTrue
244
+
245
+
246
+
247
+ Set shape = Nothing
248
+
249
+
250
+
251
+ End Sub
252
+
253
+
254
+
255
+ ```