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

3

在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

从我所了解的情况来看,这个错误与创建PowerPoint对象有关。我不确定它是否与引用有关,但我尝试打开PowerPoint应用程序时会收到429错误。 - Cyril
在我的情况下,我已将“dim objPPT As PowerPoint.Application”修改为“Dim objPPT As Object”,那么PowerPoint可以正常打开,但是我无法从网站上打开文件。抱歉,这就是我能提供的帮助了。 - D. O.
在SharePoint中跳过身份验证过程可能会很困难,因为每个站点、列表、文件夹等都有特定的用户权限。您是否尝试使用WebDAV地址将文档库映射到驱动器号,以便在代码中访问该库?这里有一个类似的问题:http://stackoverflow.com/questions/12217680/open-sharepoint-excel-files-with-vba - Part_Time_Nerd
我之前在尝试映射驱动器时遇到了一个位置的问题,所以我一直避开它。不过这是个好建议。 - pheeper
1个回答

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

在打开PowerPoint文件时,是否可以提供用户名和密码?实际上,PowerPoint仅可用于查看,只有一个用户可以编辑该文件,我想将该用户名和密码绑定到代码中,以便能够以编程方式打开ppt并进行一些修改。 - Shubham
1
不,你不能简单地传递用户名和密码以打开文件,因为PowerPoint无法处理认证过程。您唯一能处理此问题的方式是与网站进行交互,这意味着创建一个子例程来查找特定窗口,然后输入用户名和密码并点击提交。但是,如果当前用户已经登录到该网站,则首先需要将其注销,然后他们必须手动重新登录。如果您找到其他方法,请回复此帖。 - pheeper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接