我有一个很多用户都在使用的 xlsm 文件,我添加了一个更新功能,该功能需要在服务器上检查 xlsm 文件的新更新是否可用,如果可用,则需要下载该文件,并且然后覆盖现有文件,我如何得到一个错误写入文件失败错误 3004 任何人都可以帮助我吗?
让我解释一下我的代码;客户端 xlsm 文件有一个检查新更新按钮,当用户单击该按钮时,会发生以下情况,
Private Sub CommandButton5_Click()
Dim Answer As VbMsgBoxResult, N%, MyFile$
Answer = MsgBox("1) You need to be on-line to update" & vbLf & _
"2) The update may take a few minutes" & vbLf & _
"3) Please do not interrupt the process once started" & vbLf & _
"" & vbLf & _
"SEARCH FOR UPDATE?", vbYesNo, "Update?")
If Answer = vbNo Then Exit Sub
'otherwise - carry on
Application.ScreenUpdating = False
Application.EnableCancelKey = xlDisabled
On Error GoTo ErrorProcedure
Application.Workbooks.Open ("http://www.mysite.com/Download/Update.xlsm")
'The book on the site opens and you can do whatever you
'want now (note that the remote book is "Read Only") - in
'this particular case a workbook_Open event now triggers
'a procedure to export the new file to the PC
ErrorProcedure:
MsgBox Err.Description
End Sub
然后从服务器打开 update.xlsm,这里是代码;
Private Sub workbook_open()
Dim localfile As Date
Dim newfile As Date
localfile = FileDateTime("C:\Documents and Settings\localhost\Desktop\sample.xlsm")
newfile = "6/6/2013 4:00"
If DateDiff("s", localfile, newfile) > 0 Then
MsgBox "its closed"
Application.StatusBar = "contacting the download"
Dim myURL As String
myURL = "http://www.mysite.com/Download/sample.xlsm"
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
WinHttpReq.Send
Application.StatusBar = "waiting for the response"
myURL = WinHttpReq.ResponseBody
If WinHttpReq.Status = 200 Then
Application.DisplayAlerts = False
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.ResponseBody
oStream.SaveToFile ("C:\Documents and Settings\localhost\Desktop\sample.xlsm")
oStream.Close
End If
MsgBox "Update Completed"
Application.StatusBar = ""
Windows("Update.xlsm").Activate
ActiveWindow.Close
Application.DisplayAlerts = True
Else
MsgBox "There is no New Update"
Application.StatusBar = ""
End If
End Sub