我有一個正在被很多用戶使用的xlsm文件,我添加了一個更新函數,需要在服務器上檢查xlsm文件的新更新是否可用,以及如果它可用它需要下載文件,然後覆蓋現有的文件,一些如何得到一個錯誤寫入文件失敗的錯誤3004任何人都可以幫助我嗎?VBA將新文件寫入Program Files文件夾
讓我解釋我的代碼; 客戶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
此頁面可能會引起您的興趣,並給您一種不同的方法:http://www.excelguru.ca/content.php?152-Deploying-Add-ins-in-a-Network-Environment。 –