【问题标题】:VBA Write new file to Program Files folderVBA 将新文件写入 Program Files 文件夹
【发布时间】:2013-06-06 01:44:46
【问题描述】:

我有一个很多用户都在使用的 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

【问题讨论】:

  • 此页面可能会引起您的兴趣,并为您提供一些不同的方法:excelguru.ca/…

标签: vba excel


【解决方案1】:

写入%PROGRAMFILES% 需要在 Windows Vista 及更高版本(或以受限用户身份运行时的 XP)上的管理权限。应用程序不应该在那里存储它们的数据,而这些信息已经发布了十多年。

Does Microsoft have a best practices document regarding the storage of App Data vs User Data on different Windows Platforms?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. localhost 极不可能有 Desktop 文件夹,即使您在正确的位置查找用户文档也是如此。 localhost 是 IP 地址 127.0.0.1 的别名,我从不知道 IP 地址别名的桌面文件夹。 localhost不是本机用户,只有用户才能拥有桌面文件夹。

【讨论】:

    猜你喜欢
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 2015-04-28
    • 2012-07-12
    相关资源
    最近更新 更多