我有一个电子表格,其中创建了一些宏来从一列中获取图片网址以在另一列中显示。 当我运行宏时,它会顺利进行,直到我达到类似这些宏的URL:
http://ecx.images-amazon.com/images/I/513aNHvKu3L.SL160.jpg
然后我收到运行时错误,程序停止工作。 当我点击这些网址或将它们粘贴到浏览器中时,会出现一个图像。
有没有办法让宏对这些网址更加宽容?所以,我可以从中提取图像。
这是我正在使用的代码:
Sub InstallPictures()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim url_column As Range
Dim image_column As Range
'column with url...
Set url_column = Worksheets(1).UsedRange.Columns("V")
'column where image will be inserted
Set image_column = Worksheets(1).UsedRange.Columns("W")
Dim i As Long
For i = 2 To url_column.Cells.Count
Set Picture = image_column.Worksheet.Pictures.Insert(url_column.Cells(i).Value)
Picture.Left = image_column.Cells(i).Left
Picture.Top = image_column.Cells(i).Top
Picture.Height = 40
image_column.Cells(i).EntireRow.RowHeight = 40
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
答案 0 :(得分:1)
<强> 背景 强>
虽然我可以重现错误,但我不明白为什么在这两个URL中特别发生这种情况;我认为中的任何一个条件可能正在发生:
a)它可能与“。”有关。和“,”
b)用于这些站点的DDoS保护 - 我的猜测 -
免责声明:这是一种解决方法
这应该记录当前无法检索的地址 - 并继续运行可以 - 。
Sub InstallPictures()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim url_column As Range
Dim image_column As Range
'column with url...
Set url_column = Worksheets(1).UsedRange.Columns("V")
'column where image will be inserted
Set image_column = Worksheets(1).UsedRange.Columns("W")
Dim i As Long
For i = 2 To url_column.Cells.Count
On Error GoTo Err01InstallPictures
Set Picture = image_column.Worksheet.Pictures.Insert(url_column.Cells(i).Value)
Picture.Left = image_column.Cells(i).Left
Picture.Top = image_column.Cells(i).Top
Picture.Height = 40
image_column.Cells(i).EntireRow.RowHeight = 40
If 1 = 2 Then '99. If error only falls here if error is detected
Err01InstallPictures:
image_column.Cells(i).Value = url_column.Cells(i).Value
image_column.Cells(i).Interior.Color = vbRed
On Error GoTo -1
End If '99. If error only falls here if error is detected
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
更多详情
我尝试的唯一额外的事情是使用宏开发人员来查看为什么它在正常界面中执行它,但是很遗憾,VBA没有确定为什么会发生这种情况的结论。