2013-06-06 66 views
0

我有一個正在被很多用戶使用的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 
+0

此頁面可能會引起您的興趣,並給您一種不同的方法:http://www.excelguru.ca/content.php?152-Deploying-Add-ins-in-a-Network-Environment。 –

回答

2

寫入%PROGRAMFILES%要求(以受限用戶身份運行時,或XP)在Windows Vista和上述管理權限。應用程序不應該在那裏存儲他們的數據,而且這些信息已經發布了十多年了。

一個很好的參考這裏瞭解在哪裏存儲應用程序的數據信息在Does Microsoft have a best practices document regarding the storage of App Data vs User Data on different Windows Platforms?

然而,你的問題是混亂的,因爲你在你的主題是指Program Files folder,但你的代碼使用硬編碼路徑C:\Documents and Settings\localhost\Desktop,這是不一樣的事情。如果是這樣的實際問題,這可能是因爲兩個問題:

  1. 您已經硬編碼在C:\Documents and Settings,這已不再是用戶數據的正確位置,因爲Windows Vista中被釋放。您應該使用可用的WinAPI函數來查找該文件夾。 (搜索在這裏爲SO [winapi] SHGetFolderLocation

  2. 您已經硬編碼在爲用戶的Desktop文件夾,這再次可能不是,你認爲應該的位置。您使用上面的搜索找到的相同WinAPI功能應該用於查找桌面文件夾。

  3. 這是極不可能的localhostDesktop文件夾,即使你正在尋找在用戶的文件在正確的地方。 localhost是IP地址127.0.0.1的別名,我從來不知道IP地址別名的桌面文件夾。 localhost不是本地計算機上的用戶,只有用戶可以擁有桌面文件夾。

相關問題