尝试使用 VBA 在网络浏览器控件上执行 javascript 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4696550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:31:21  来源:igfitidea点击:

Trying to use VBA to execute a javascript command on a webbrowser control

javascriptvbawebbrowser-control

提问by Chaz

I am trying to execute the command toggletable or showall function on a javascript page that has the tables information hidden until you click on the + button next to it. I would like to just make these automaticly expanded for printing purposes. Here is what I have so far.

我正在尝试在隐藏表格信息的 javascript 页面上执行命令 toggletable 或 showall 功能,直到您单击它旁边的 + 按钮。我只想让这些自动扩展用于打印目的。这是我到目前为止所拥有的。

Function PrintWebPage()
Const OLECMDID_PRINT = 6
Const OLECMDEXECOPT_PROMPTUSER = 1
Const OLECMDEXECOPT_DONTPROMPTUSER = 2

Dim ie As Object
Dim strWebPage As String, stblAutoNumber(99999) As String, stblBadgeNumber(999999) As String, stblShopNumber(99999) As String

DoCmd.SetWarnings False
' Connect to DB
Set db = CurrentDb()

' Select Statement for scrolling through everyone
sqlString = "SELECT tblPersonal.AutoNumber, tblPersonal.[Badge Number], tblPersonal.Shop , tblPersonal.[Last Name] FROM tblPersonal WHERE tblPersonal.[Shop] = " & """" & ShopUserATMS & """" & ";"

' Sets mRecordset to query the database
Set mRecordset = db.OpenRecordset(sqlString)

' Goes to first record of the generated list
mRecordset.MoveFirst
Do While Not mRecordset.EOF
    ' Scroll through personal List
    stblAutoNumber(i) = mRecordset("AutoNumber")
    CheckBadgeNull = mRecordset("Badge Number")
    If IsNull(CheckBadgeNull) = True Then
        GoTo NoRec:
    End If
    stblBadgeNumber(i) = mRecordset("Badge Number")
    stblShopNumber(i) = mRecordset("Shop")
    strWebPage = "https://was3.nnsy.navy.mil/atms/components/supervisor/atms_supv_detail.cfm?BADGE=" & stblBadgeNumber(i)
    DoEvents: DoEvents: DoEvents
    Set ie = CreateObject("internetexplorer.application")


    ie.Navigate strWebPage

    Do Until ie.Busy = False
        sSleep (1)
    Loop
    Call ie.Document.parentWindow.execScript("toggletable(Quals)", "JavaScript")
    'ie.getelementsbyid("Showall") = True
    'stblShopNumber(99) = ie.Document.execcommand("toggletable", False, Null)
    ie.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER

    sSleep (2)
NoRec:

Loop

    ie.Quit

    Set ie = Nothing

End Function

It gives me access denied when I use the following command: Call ie.Document.parentWindow.execScript("toggletable(Quals)", "JavaScript")

当我使用以下命令时,它让我拒绝访问:调用 ie.Document.parentWindow.execScript("toggletable(Quals)", "JavaScript")

Any help is appreciated. Beating my head on this one for over 8 hours...

任何帮助表示赞赏。在这个问题上敲我的头超过 8 个小时......

回答by sidekickbottom

You need to enable "Allow active content to run in files on My Computer" option from Internet Options-->Advanced-->Security. If you want to change this IE setting programmatically, below is the vbscript code for it:

您需要从 Internet 选项--> 高级--> 安全中启用“允许活动内容在我的电脑上的文件中运行”选项。如果您想以编程方式更改此 IE 设置,以下是它的 vbscript 代码:

Const HKEY_CURRENT_USER = &H80000001    
strComputer = "."
dwValue = 0
Set objReg = GetObject("winmgmts:" & "{impersonationLevel=impersonate}\" & strComputer & "\root\default:StdRegProv")   
strKeyPath = "Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_LOCALMACHINE_LOCKDOWN"
objReg.SetDWORDValue HKEY_CURRENT_USER, strKeyPath,"iexplore.exe",dwValue

set dwValue to 0 to enable, and 1 to disable the IE setting.

将 dwValue 设置为 0 以启用,设置为 1 以禁用 IE 设置。

回答by DontFretBrett

Dim IE As New InternetExplorer
IE.Visible = True
IE.navigate "http://www.google.com"
Do: DoEvents: Loop Until IE.readyState = READYSTATE_COMPLETE
IE.navigate "javascript:alert('hi');"