使用 VBA 获取或设置 Internet Explorer 选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20642519/
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-12 01:00:02  来源:igfitidea点击:

Getting or Setting Internet Explorer Options with VBA

excelinternet-explorervbaexcel-vba

提问by Soulfire

I am working within an Excel workbook that calls internet explorer and directs the workbook user to a log-in page in order to validate them and then retrieve data from our server. I have found that if users have 'Protected Mode' (Internet Options --> Security) enabled in IE they will often get "stuck" on the log-in page after entering their username and password. When 'Protected Mode' is disabled, there is no issue.

我在 Excel 工作簿中工作,该工作簿调用 Internet Explorer 并将工作簿用户定向到登录页面以验证它们,然后从我们的服务器检索数据。我发现如果用户在 IE 中启用了“保护模式”(Internet 选项 --> 安全),他们通常会在输入用户名和密码后“卡住”在登录页面上。禁用“保护模式”时,没有问题。

I am trying to create a macro that will detect their IE settings and notify the user if protected mode is enabled (and potentially change this setting for them with VBA using the PutPropertymethod, if possible). I am currently working with Microsoft Internet Controls reference. The route I have attempted is below (within a subroutine, of course).

我正在尝试创建一个宏来检测他们的 IE 设置并通知用户是否启用了保护模式(PutProperty如果可能的话,可能会使用该方法使用 VBA 为他们更改此设置)。我目前正在使用 Microsoft Internet Controls 参考。我尝试的路线如下(当然是在子程序中)。

Dim objBrowser As InternetExplorer
Dim vProtected As Variant

Set objBrowser = New InternetExplorer

'Opens IE and navigates to log-in page
'The code here works as expected 

'No idea what to put in for arguments in GetProperty
vProtected = objBrowser.GetProperty("?")

I am unsure of what data type GetPropertyactually returns (hence the variant) and I do not know what to pass in it for arguments. Additionally, if this is the wrong way entirely to do this I am open to a new course of action.

我不确定GetProperty实际返回什么数据类型(因此是变体),我不知道要传入什么作为参数。此外,如果这完全是错误的做法,我愿意采取新的行动方针。

The MSDN articleon the GetProperty method of the InternetExplorer object was less than helpful.

关于 InternetExplorer 对象的 GetProperty 方法的MSDN 文章没有什么帮助。

It is worth mentioning I am quite new to Microsoft Internet Controls and the InternetExplorer object, this is the first time I've worked with it.

值得一提的是,我对 Microsoft Internet Controls 和 InternetExplorer 对象还很陌生,这是我第一次使用它。

Thank you for your time and consideration!

感谢您的时间和考虑!

回答by Soulfire

The solution ended up being vastly different than what I was going for. I have resigned myself to the fact that I must play with the registry in order to achieve the behavior I want. Though this was undesirable, here is what I ended up doing in VBA.

解决方案最终与我想要的大不相同。我已经接受了这样一个事实,即我必须使用注册表才能实现我想要的行为。尽管这是不可取的,但这是我最终在 VBA 中所做的。

This is not my final code, as I am still working on the project, but it suffices.

这不是我的最终代码,因为我仍在处理该项目,但这已经足够了。

Dim obj_Shell
Dim v_Result As Variant

'Variable to capture user response
Dim v_Response As Variant

Set obj_Shell = CreateObject("WScript.Shell")

'CHECK INTERNET EXPLORER PROTECTED MODE SETTINGS

'Reads the registry key that determines whether 'Protected Mode' is enabled in internet explorer for the 'Internet' security zone
'as opposed to 'Local intranet' or 'Trusted Sites', etc.
'The 3 in the registry key path refers to the zone 'Internet'.

Debug.Print "Checking user's Internet Explorer protected mode settings..."

v_Result = obj_Shell.RegRead _
("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones00")

Select Case v_Result
    Case 0 'Protected Mode is enabled
        Debug.Print "   Protected mode is enabled!"
        v_Response = MsgBox("Protected mode is enabled in Internet Explorer.  " & _
        "This may cause issues with your submission.  " & _
        "Would you like to disable protected mode?", vbYesNo, "Protected Mode Enabled")

        If v_Response = vbYes Then
            obj_Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones00", 3, "REG_DWORD"
            MsgBox ("Protected mode has been disabled!  Submission will not proceed.")
        End If

    Case 3 'Protected Mode is disabled
        Debug.Print "   Protected mode is disabled"
    Case Else
        Debug.Print "Unable to determine status of protected mode in IE"
        Debug.Print v_Result
End Select

回答by MikeD

I don't think you can use GetProperty to read out the browsers' security settings. From the doc:

我认为您不能使用 GetProperty 来读取浏览器的安全设置。从文档:

You may want to explore the protected mode API described here, or the URL Security Zones API described here.

你可能想探索保护模式API描述这里,或URL安全区域API描述这里