从SharePoint使用Excel VBA 2016打开Powerpoint文件

时间:2017-03-06 18:16:25

标签: vba excel-vba sharepoint excel

在Excel 2010中使用VBA我可以通过传递文件的URL位置在SharePoint上打开PowerPoint文件。如果我没有登录,它会提示我输入我的凭据,以便打开它。

但是,在Excel 2016中,当我收到在SharePoint上打开Excel文件的提示时,我在打开PowerPoint文件时没有收到提示。相反,我只是得到一个运行时错误' -2147467259(80004005)'意思未经认证。如果我首先登录SharePoint然后运行它打开的宏,但我不想这样做。有关如何将提示返回PowerPoint的任何建议?

Sub OpenPowerPointFile()
    Dim objPPT As PowerPoint.Application
    Dim objPres As Object
    Set objPPT = CreateObject("Powerpoint.Application")
    objPPT.Visible = True

    Set objPres = objPPT.Presentations.Open("https://spsite.com/report_template.pptx")

End Sub

1 个答案:

答案 0 :(得分:1)

我能够通过打开文件,循环遍历所有打开的PPT文档来查找特定的文件名,然后将其分配给对象变量来解决这个问题。

Private Function openPowerPointPresentationFromURL(filePath As String, fileName As String) As PowerPoint.Presentation

    Dim ppProgram As PowerPoint.Application
    Dim ppCount As Integer
    Dim i As Integer

    On Error GoTo ErrHandler

    ' Open the file 
    ThisWorkbook.FollowHyperlink (filePath)        

    ' Set the object by looping through all open PPT files and look for the passed file name
    Set ppProgram = GetObject(, "PowerPoint.Application")            
    ppCount = ppProgram.Presentations.Count
    For i = 1 To ppCount + 1
        If ppProgram.Presentations.Item(i).Name = fileName Then
            Set openPowerPointPresentationFromURL = ppProgram.Presentations.Item(i)
            Exit Function
        End If
    Next i
    On Error GoTo 0

ErrHandler:
    MsgBox "There was an error opening the PowerPoint template that is required for this report."
    On Error GoTo 0

End Function