Using IWebBrowser2 to put Widgets in PowerPoint — Automation Error: Unspecified Error

雨燕双飞 提交于 2019-12-11 13:56:16

问题


I created a simple macro to put an HTML widget from Weather.com in to a PowerPoint slide.

It is not event-based, but ActionButtons call the ConnectWidget subroutine, when navigating to the slide. That's working fine, but you will notice that I have only been able to get this to work by first deleting the existing WebBrowser shape, and then re-creating it.

I had to do this, ultimately, because any method call from wb after the .Navigate, I get an Automation Error/Unspecified Error, or Method unavailable error. So for example, I could not call on wb.Refresh when moving back and forth between slides, without first deleting the browser shape entirely, and recreating it.

There is probably something obvious that I am overlooking -- any thoughts to what might be causing those errors? Google turned up nothing really useful in resolving the problem.

Sub ConnectWidget()
    Dim sld As Slide
    Dim pres As Presentation
    Dim shp As Shape
    Dim wb As IWebBrowser2
    Dim fname As String: fname = "c:" & Environ("homepath") & "\widget2.html"
    Set pres = ActivePresentation
    Set sld = pres.Slides(2)

    On Error Resume Next
    sld.Shapes("weatherwidget").Delete
    On Error GoTo 0

    If Len(Dir(fname)) = 0 Then CreateHTML fname

    Set shp = sld.Shapes.AddOLEObject(100, 200, 200, 150, _
        "Shell.Explorer.2")
    shp.Name = "weatherwidget"
    Set wb = sld.Shapes("weatherwidget").OLEFormat.Object

    With wb
        .Navigate (fname)
    End With

    ' wb.Refresh  '## UNCOMMENT THIS LINE AND YOU WILL GET AN ERROR

    Set wb = Nothing
    On Error Resume Next
    Kill fname
    On Error GoTo 0

End Sub

Private Sub CreateHTML(fileName$)
    'Createsa plaintext HTML file that IWebBrowser2 can navigate
    '<script type="text/javascript" src="http://voap.weather.com/weather/oap/90210?template=GENXH&par=3000000007&unit=0&key=twciweatherwidget"></script>
    Dim htmlTxt As String
    Dim fs As Object
    Dim a As Object
    htmlTxt = "<script type=" & Chr(34) & "text/javascript" & Chr(34) & "src=" & Chr(34) & _
            "http://voap.weather.com/weather/oap/90210?template=GENXH&par=3000000007&unit=0&key=twciweatherwidget" _
            & Chr(34) & "></script>"

        Set fs = CreateObject("Scripting.FileSystemObject")
        Set a = fs.CreateTextFile(fileName, True)
        a.WriteLine htmlTxt
        a.Close

End Sub

Here is one such error. Note that I have no option to Debug the error. All I can surmise from stepping over the code is that any method call after the .Navigate will result in this error.

来源:https://stackoverflow.com/questions/16992620/using-iwebbrowser2-to-put-widgets-in-powerpoint-automation-error-unspecified

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!