none
unable call javascript from vba in IE11 RRS feed

  • Question

  • I am unable call javascript from Word macro (VBA) in IE 11. IeObject.Paraentwindow.excescript  showing access Denied. Then I try 

    IeObject.navigate .... but its showing busy status of the URL.  URL means which URL have java script .

    urgent plz help anyone. Thanks:Rajesh Bisht

    Tuesday, December 23, 2014 6:02 PM

All replies

  • Hi Rajesh Bisht,

    Thanks for posting in MSDN forum.

    This forum is for developers for discussting developing issues involve Word application. For example, use Word object model.

    Since the issue is more reltative to VBA, I would move it to VBA forum to get more effectve response.

    Also I suggest that you post a demo code to reprodce the issue to help other community to understant this issue.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, December 24, 2014 9:39 AM
    Moderator
  • After spend 6 hours I got solution. Code also included below :

    Call java script from VBA / VB or any other language.

    Step 1 : Get Iexplorer instance (Which URL have java script)

    Step 2:  suppose document have DIV / PARA or any element  id "#HereJsCall" 

    Step 3: Now use getelementbyid("#HereJsCall") 

    Step 4: Add run time onclick="MyJsFunction" to this id.

    Step 4: call js function.

    Example: Let I want call JS Function "MyJsFunction" URL is : "https://ops.leafletcorp.com:9010/document.htm"

    string   urlname = "https://ops.leafletcorp.com:9010/document.htm"

            Object IeInstance = getIEInstance(urlname)

            object ElementObject =  IeInstance.Document.getelementbyid("#HereJsCall")

           ElementObject.OuterHtml =  ElementObject.OuterHtml + (Add onclick code )

           ElementObject.Click

    ****************** Get Instance of the Given URL ************************

     Public Function getIEInstance(sHTMLPageName As String) As Object
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    '  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        Dim objInstances As Object, objIE As Object
        Dim sname As String
        
        Set objInstances = CreateObject("Shell.Application").Windows
        Set getIEInstance = Nothing
        If objInstances.Count > 0 Then '/// make sure we have instances open.
        Err.Clear
       
        
            For Each objIE In objInstances
    On Error Resume Next
                sname = objIE.FullName
             
    On Error GoTo errhandler
                If UCase(Right(sname, 12)) = "IEXPLORE.EXE" Then '/// it's internet explorer not windows explorer.
                    'If InStr(1, LCase(objIE.locationURL), LCase(sHTMLPageName)) <> 0 Then
                        Set getIEInstance = objIE
                        Exit For
                   ' End If
                End If
                sname = ""
            Next
        End If
        
    Exit Function
        
    errhandler:
    If Err.Number <> 0 Then
       MsgBox Err.Description + " " + sname, vbOKOnly, " getIEInstance"
    End If
      
    End Function

    Tuesday, January 6, 2015 7:03 PM
  •  Code in VBA : But this Logic you can apply in any language 

    And this logic will work for any kind of Browser....

    After spend 6 hours I got solution. Code also included below :

    Call java script from VBA / VB or any other language.

    Step 1 : Get Iexplorer instance (Which URL have java script)

    Step 2:  suppose document have DIV / PARA or any element  id "#HereJsCall" 

    Step 3: Now use getelementbyid("#HereJsCall") 

    Step 4: Add run time onclick="MyJsFunction" to this id.

    Step 4: call js function.

    Example: Let I want call JS Function "MyJsFunction" URL is : "https://ops.leafletcorp.com:9010/document.htm"

    string   urlname = "https://ops.leafletcorp.com:9010/document.htm"

            Object IeInstance = getIEInstance(urlname)

            object ElementObject =  IeInstance.Document.getelementbyid("#HereJsCall")

           ElementObject.OuterHtml =  ElementObject.OuterHtml + (Add onclick code )

           ElementObject.Click

    ****************** Get Instance of the Given URL ************************

     Public Function getIEInstance(sHTMLPageName As String) As Object
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    '  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        Dim objInstances As Object, objIE As Object
        Dim sname As String
        
        Set objInstances = CreateObject("Shell.Application").Windows
        Set getIEInstance = Nothing
        If objInstances.Count > 0 Then '/// make sure we have instances open.
        Err.Clear
       
        
            For Each objIE In objInstances
    On Error Resume Next
                sname = objIE.FullName
             
    On Error GoTo errhandler
                If UCase(Right(sname, 12)) = "IEXPLORE.EXE" Then '/// it's internet explorer not windows explorer.
                    'If InStr(1, LCase(objIE.locationURL), LCase(sHTMLPageName)) <> 0 Then
                        Set getIEInstance = objIE
                        Exit For
                   ' End If
                End If
                sname = ""
            Next
        End If
        
    Exit Function
        
    errhandler:
    If Err.Number <> 0 Then
       MsgBox Err.Description + " " + sname, vbOKOnly, " getIEInstance"
    End If
      
    End Function

    Tuesday, January 6, 2015 7:05 PM