2016-01-04 102 views
2

我正在用VBA從Web上下載圖像到本地機器,然後將該圖像移動到服務器。此外,我將該圖像壓縮爲縮略圖大小,並將該文件移動到服務器。VBA等待文件下載

在這兩種情況下,我收到以下錯誤,當我嘗試將文件移動到服務器:

Run-time error '75': Path/File access error

我曾嘗試使用下面的代碼要等到該文件存在,將它移到嘗試:

Do While Dir(sLocalFile & ".jpg") = "" 
    DoEvents 
Loop 

但我仍然收到錯誤消息。

此外,如果我單擊「調試」按鈕並立即單擊「繼續」推F5,宏即可順利執行而不會出現問題,表明即使Dir指示文件存在,但它尚未準備好移動(即可能仍在寫入)。因此,我試圖用下面的代碼來繼續嘗試移動文件直到它沒有錯誤這樣做:

MoveFile: 
    On Error GoTo MoveFile 
    Name sLocalFile & ".jpg" As ServPath & PartNum & ".jpg" 
    On Error GoTo 0 

不過,我還是得到了同樣的錯誤(我也嘗試切換標籤和On Error命令,但這並沒有改變任何東西)。

編輯:這裏是我使用的下載文件中的函數:我在網上找到的地方

Function DownloadFile(sSourceURL As String, _ 
    sLocalFile As String _ 
) As Boolean 
    DownloadFile = URLDownloadToFile(0&, _ 
            sSourceURL, _ 
            sLocalFile, _ 
            BINDF_GETNEWESTVERSION, _ 
            0& _ 
            ) = ERROR_SUCCESS 
End Function 

,並有只有它在做什麼一個基本的瞭解。

編輯2:我要補充一點,宏從運行SolidWorks內和在情況下,有沒有爲對象的圖像可在網上,我用下面的代碼的SolidWorks生成圖像:

Dim sUrl As String, sLocalFile As String 
sUrl = UserFormImportPart.TextBoxImgURL 
sLocalFile = "C:\temp\" & UserFormImportPart.TextBoxPartNum 
If sUrl = "" Then 
    With swApp 
     .SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swExportJpegCompression, 100 
     .SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swTiffScreenOrPrintCapture, 1 
     .SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swTiffPrintDPI, 400 
    End With 
    With swModel 
     .ShowNamedView2 "*Isometric", 7 
     .ViewZoomtofit2 
     .Extension.InsertScene "\scenes\01 basic scenes\11 white kitchen.p2s" 
     .SaveAs3 sLocalFile & ".jpg", 0, 0 
     .Extension.InsertScene "\scenes\01 basic scenes\00 3 point faded.p2s" 
     If Len(Dir(sLocalFile)) > 0 Then 
      Kill sLocalFile 
     End If 
     Name sLocalFile & ".jpg" As sLocalFile 
    End With 
Else 
    DownloadFile sUrl, sLocalFile 
End If 

當我正在下載文件以及何時使用SolidWorks生成文件時,會發生錯誤。

+0

究竟你是如何下載文件? –

+0

在我的帖子末尾查看我的編輯。 – tlewis3348

+0

我只記得我並不總是下載圖像。這是從SolidWorks內部運行的,如果由於某種原因,網上沒有可用的圖像,我將從SolidWorks模型生成圖像。無論圖像的來源如何,都會發生同樣的錯誤。我會更新這個問題來反映這一點。 – tlewis3348

回答

0

基於@ NeepNeepNeep的評論,我能想出這個,這似乎是工作得相當好:

Do While Dir(sLocalFile & ".jpg") = "" 
    DoEvents 
Loop 
Sleep 10 
NewLen = 1 
Do While NewLen > OldLen 
    OldLen = NewLen 
    NewLen = FileLen(sLocalFile & ".jpg") 
    If NewLen < OldLen Then 
     NewLen = OldLen + 1 
    End If 
Loop 
OldLen = 0 

Name sLocalFile & ".jpg" As ServPath & PartNum & ".jpg"