1

我有从网站下载图片的 Excel VBA 代码。

如果网络主机未启动或未完成提供图像,则代码无限期挂起,显示“正在下载”用户对话框。您可以单击“取消”,但随后您需要重新启动 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
4

0 回答 0