VBA:从服务器获取/下载Excel文件并将内容复制到新的工作表中

由于某些原因,下面的macros将不起作用:

Sub ExtractDataTest() Dim FilePath$, Row&, Column&, Address$ 'change constants & FilePath below to suit '*************************************** Const FileName$ = "Dxo.xlsx" Const SheetName$ = "Open" Const NumRows& = 50 Const NumColumns& = 20 FilePath = ("http://enhanced1.sharepoint.hx.com/teams/") '*************************************** DoEvents Application.ScreenUpdating = False If Dir(FilePath & FileName) = Empty Then MsgBox "The file " & FileName & " was not found", , "File Doesn't Exist" Exit Sub End If For Row = 1 To NumRows For Column = 1 To NumColumns Address = Cells(Row, Column).Address Cells(Row, Column) = GetData(FilePath, FileName, SheetName, Address) Columns.AutoFit Next Column Next Row ActiveWindow.DisplayZeros = False End Sub Private Function GetData(Path, File, Sheet, Address) Dim Data$ Data = "'" & Path & "[" & File & "]" & Sheet & "'!" & _ Range(Address).Range("A1").Address(, , xlR1C1) GetData = ExecuteExcel4Macro(Data) End Function 

在线(“坏文件名或号码”)我得到一个运行时错误“52”:

 If Dir(FilePath & FileName) = Empty Then 

有趣的是,它曾经工作过一次。 而当位置是“C:\”它的工作,我不会得到一个错误。 奇怪的东西家伙。

任何帮助将不胜感激。

从文件系统打开文件与通过HTTP下载完全不同。 最简单的不可知的方法是简单地使用Workbooks.Open ,它允许HTTP URI;

 Set wb = Workbooks.Open(FilePath & FileName) 

(您只需要将文件系统的Dir()删除)