使用VBA从Sharepoint下载excel加载项
我有一个excel文件,打开时需要下载并打开存储在Sharepoint中的加载项的最新版本。我有一段代码可以下载外接程序,将其保存在特定位置(strSavePath)并尝试打开它使用VBA从Sharepoint下载excel加载项,sharepoint,Sharepoint,我有一个excel文件,打开时需要下载并打开存储在Sharepoint中的加载项的最新版本。我有一段代码可以下载外接程序,将其保存在特定位置(strSavePath)并尝试打开它 Function funLoadRomeFiles(strURL As String, strSavePath As String) Dim objConnection As Object Dim objStream As Object Set objConnection = CreateObject("M
Function funLoadRomeFiles(strURL As String, strSavePath As String)
Dim objConnection As Object
Dim objStream As Object
Set objConnection = CreateObject("MSXML2.ServerXMLHTTP.6.0")
On Error GoTo ExitConnect
objConnection.Open "GET", strURL, False
objConnection.send
strURL = objConnection.responseBody
If objConnection.Status = 200 Then
Set objStream = CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.Write objConnection.responseBody
objStream.SaveToFile strSavePath, 2
objStream.Close
End If
ExitConnect:
On Error GoTo 0
Shell "C:\WINDOWS\explorer.exe """ & strSavePath & "", vbHide
End Function
但是,我在倒数第二行得到一个错误。错误是:Excel无法打开文件“Filename”,因为文件格式或文件扩展名无效[…]”。下载的文件已损坏,也无法手动打开。当我下载并手动打开它时,它工作正常
文件大小为30.9 kb,但执行代码会将其作为51 kb文件下载。我曾尝试使用此代码下载其他文件,但它们也已损坏,并且无论实际文件大小为51 kb。是否有任何方法更改代码以使文件不会损坏,或者有任何其他方法执行此操作
更新:下载的文件似乎是一个html文件,尽管它的名称仍然以.xlam结尾
另外,我尝试过使用以“filename.xlam”结尾的链接和以“filename.xlam?csf=1&e=b5f7991021ab45c1833229210f3ce810”结尾的链接,两者都给出了相同的结果,当您将链接复制到chrome中时,两者都会立即下载正确的文件,我曾经遇到过类似的问题 我遇到的问题是,sharepoint不允许某种类型的文件。因此我必须做一个变通办法。因此,您可以尝试压缩您的*.xlam文件,并将其放在sharepoint上。然后,您可以使用已有的代码下载该文件。然后,您只需使用以下代码解压缩即可
Sub Unzip1()
Dim FSO As Object
Dim oApp As Object
Dim Fname As Variant
Dim FileNameFolder As Variant
Dim DefPath As String
Dim strDate As String
Fname = strSavePath' I assume that this is the Path to the File you Downloaded
If Fname = False Then
'Do nothing
Else
'Root folder for the new folder.
DefPath = Application.DefaultFilePath 'Or Change it to the Path you want to unzip the Files
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If
'Create the folder name
strDate = Format(Now, " dd-mm-yy h-mm-ss")
FileNameFolder = DefPath & "MyUnzipFolder " & strDate & "\"
'Make the normal folder in DefPath
MkDir FileNameFolder
'Extract the files into the newly created folder
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items
'If you want to extract only one file you can use this:
'oApp.Namespace(FileNameFolder).CopyHere _
'oApp.Namespace(Fname).items.Item("test.txt")
MsgBox "You find the files here: " & FileNameFolder
On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
End If
End Sub
Sub Unzip1()
作为对象的Dim FSO
作为对象的Dim oApp
Dim Fname作为变体
Dim FileNameFolder作为变量
将路径设置为字符串
作为字符串的Dim strDate
Fname=strSavePath'我假设这是您下载的文件的路径
如果Fname=False,则
“什么也不做
其他的
'新文件夹的根文件夹。
DefPath=Application.DefaultFilePath'或将其更改为要解压缩文件的路径
如果正确(DefPath,1)“\”则
DefPath=DefPath&“\”
如果结束
'创建文件夹名称
标准日期=格式(现在为“dd-mm-yy h-mm-ss”)
FileNameFolder=DefPath&“MyUnzipFolder”&strDate&“\”
'在DefPath中创建普通文件夹
MkDir文件名文件夹
'将文件解压缩到新创建的文件夹中
设置oApp=CreateObject(“Shell.Application”)
名称空间(FileNameFolder).CopyHere oApp.Namespace(Fname).items
'如果您只想提取一个文件,可以使用:
'oApp.Namespace(FileNameFolder.CopyHere_
'oApp.Namespace(Fname.items.Item(“test.txt”)
MsgBox“您可以在此处找到文件:”&FileNameFolder
出错时继续下一步
设置FSO=CreateObject(“scripting.filesystemobject”)
FSO.deletefolder环境(“Temp”)和“\Temporary Directory*”,True
如果结束
端接头
然后你就开始执行扩展了。
我希望这能对您有所帮助。我找不到下载外接程序的方法,尝试了多种不同的方法,并得出结论,我使用的SharePoint版本导致了som授权错误或其他问题。我发现适合我需要的解决方案是使用以下代码直接从SharePoint打开外接程序:
On Error Resume Next
ActiveWorkbook.FollowHyperlink Address:="strUrl"
On Error GoTo 0
试着在笔记本中打开你的文件。这是一本合适的Excel工作簿,你会看到像üGÝÝÝ)Äd/ùƒ†ýðkR这样的奇怪字符。但我怀疑你会看到一些xlm代码。如果我是对的,请告诉我。@MarcinSzaleniec我试着用记事本++打开它,它实际上是可读的。它似乎是html,它以:Logga in p开头å;ditt konto这不是VBA问题。您的sharepoint或防火墙有问题。@这是真的,也许我应该删除VBA和excel VBA标记?仍然不确定,因为希望有办法使用VBA解决此问题。您可以添加应用程序。等等(现在+TimeSerial(0,0,1)某个地方可以欺骗防火墙。但我不知道…我尝试了这个,并认为它工作了一段时间,但我不小心使用了本地文件的文件路径。结果发现任何下载的文件,.zip.txt.jpg.xlam都变成了大小为51 kb的.html文件。我想这些文件的方式可能有问题saved@JohannesAronsson嗯…然后我告诉你在加载文件的过程中出了点问题。