我有Excel VBA代码,可以从网站下载图片。
如果Web主机未启动或未提供图像,则代码将无限期挂起,并显示“正在下载”用户对话框。您可以单击“取消”,但随后需要重新启动Excel而不是正常地恢复代码。
显示“显示框”时如何正常恢复?
如果下载在10秒钟内未完成,如何超时?或者,如何更好地处理用户单击下载框上的“取消”?
我尝试过的代码段是不同的用例,无法重用。
下载框偶尔会挂在代码行ActiveSheet.Pictures.Insert(filenam).Select
Dim xRg As Range
Dim productphoto As shape
On Error Resume Next
Set Rng = ActiveSheet.Range("C2:I2")
For Each cell In Rng
filenam = cell
ActiveSheet.Pictures.Insert(filenam).Select
Set productphoto = Selection.ShapeRange.Item(1)
If productphoto Is Nothing Then GoTo lab
Set xRg = Cells(cell.Row, cell.Column)
With productphoto
.LockAspectRatio = msoFalse
.Width = 100
.Height = 100
.Top = xRg.Top + (xRg.Height - .Height) / 2
.Left = xRg.Left + (xRg.Width - .Width) / 2
End With
lab:
Set productphoto = Nothing
Application.ScreenUpdating = True
Range("C2").Select
Next