VBA Select 来自 Internet Explorer 的下拉菜单
VBA Select from dropdown in Internet Explorer
编辑:我清理了源代码格式并更新了我的 VBA 代码。我不再收到错误,但我的浏览器没有任何反应 window(没有 selected,没有点击)。
我有一些 VBA 代码允许用户在 Excel 中的表单中输入信息。该代码获取此信息并生成指向我们内部网上页面的地址。在该页面上有一个 select 导出格式的下拉列表,在选择格式后,用户可以单击以导出文件。
我试过的 VBA 代码(和几个变体)如下:
Public Sub window_Open(strLocation As String, Menubar As Boolean, height As Long, width As Long, resizable As Boolean)
With CreateObject("InternetExplorer.Application")
.Visible = False
.height = height
.width = width
.Menubar = Menubar
.Visible = True
.resizable = True
.Navigate strLocation
End With
End Sub
Private Sub OKButton_Click()
Dim ProductionAddress As String
ProductionAddress = "http://Collapsed"
window_Open ProductionAddress, True, 800, 1000, False
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate ProductionAddress
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
ie.document.getelementbyid("ReportViewerControl_ctl01_ctl05_ctl00").Value = "EXCEL"
Set objButton = ie.document.getelementbyid("ReportViewerControl_ctl01_ctl05_ctl01")
objButton.Focus
objButton.Click
End Sub
我尝试导航的页面的源代码如下。
selecting EXCEL 的元素:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<style>
body, frameset, form {
margin: 0px;
padding: 0px;
}
.PageNumberText, input, button, select, table, body, frameset {
font-size: 8pt;
font-family: Verdana;
}
.ToolbarExport {
display: inline;
}
.WidgetSet {
height: 30px;
}
.ToolBarButtonsCell {
padding-left: 6px;
}
.MenuBarBkGnd {
background-color: #ECE9D8;
}
</style>
</head>
<body style="margin: 0px; overflow: auto;"><form name="ReportViewerForm" id="ReportViewerForm" action="ReportViewer.aspx?%2fProduction%2fWell+Daily+Production&StartDate=01%2f01%2f2015+00%3a00%3a00&EndDate=07%2f21%2f2015+00%3a00%3a00&WellName=Acadia&rs%3aParameterLanguage=&rc%3aParameters=Collapsed" method="post"><table id="ReportViewerControl" style="width: 100%; height: 100%;" onclick="if (document.getElementById('ReportViewerControl_ctl00') != null) document.getElementById('ReportViewerControl_ctl00').ParametersController.HideActiveDropDown();" onactivate="if (document.getElementById('ReportViewerControl_ctl00') != null) document.getElementById('ReportViewerControl_ctl00').ParametersController.HideActiveDropDown();" cellspacing="0" cellpadding="0"><tbody><tr><td><div class="MenuBarBkGnd"><div class="ToolBarButtonsCell"><div class="ToolbarExport WidgetSet"><table style="display: inline;" cellspacing="0" cellpadding="0"><tbody><tr><td height="28"><select name="ReportViewerControl$ctl01$ctl05$ctl00" title="Export Formats" id="ReportViewerControl_ctl01_ctl05_ctl00" onchange="document.getElementById('ReportViewerControl_ctl01_ctl05_ctl01').Controller.SetViewerLinkActive(document.getElementById('ReportViewerControl_ctl01_ctl05_ctl00').selectedIndex != 0);"><option value="EXCEL">Excel</option></select></td></tr></tbody></table></div></div></div></td></tr></tbody></table></form></body>
</html>
以及导出元素的源代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<style>
.DisabledLink {
font-size: 8pt;
font-family: Verdana;
color: gray;
text-decoration: none;
cursor: default;
}
body, frameset, form {
margin: 0px;
padding: 0px;
}
.PageNumberText, input, button, select, table, body, frameset {
font-size: 8pt;
font-family: Verdana;
}
.ToolbarExport {
display: inline;
}
.WidgetSet {
height: 30px;
}
.ToolBarButtonsCell {
padding-left: 6px;
}
.MenuBarBkGnd {
background-color: #ECE9D8;
}
</style>
</head>
<body style="margin: 0px; overflow: auto;"><form name="ReportViewerForm" id="ReportViewerForm" action="ReportViewer.aspx?%2fProduction%2fWell+Daily+Production&StartDate=01%2f01%2f2015+00%3a00%3a00&EndDate=07%2f21%2f2015+00%3a00%3a00&WellName=Acadia&rs%3aParameterLanguage=&rc%3aParameters=Collapsed" method="post"><table id="ReportViewerControl" style="width: 100%; height: 100%;" onclick="if (document.getElementById('ReportViewerControl_ctl00') != null) document.getElementById('ReportViewerControl_ctl00').ParametersController.HideActiveDropDown();" onactivate="if (document.getElementById('ReportViewerControl_ctl00') != null) document.getElementById('ReportViewerControl_ctl00').ParametersController.HideActiveDropDown();" cellspacing="0" cellpadding="0"><tbody><tr><td><div class="MenuBarBkGnd"><div class="ToolBarButtonsCell"><div class="ToolbarExport WidgetSet"><table style="display: inline;" cellspacing="0" cellpadding="0"><tbody><tr><td height="28"><a title="Export" class="DisabledLink" id="ReportViewerControl_ctl01_ctl05_ctl01" onmouseover="this.Controller.OnLinkHover();" onmouseout="this.Controller.OnLinkNormal();" onclick=" var formatDropDown = document.getElementById('ReportViewerControl_ctl01_ctl05_ctl00'); if (formatDropDown.selectedIndex == 0) return false; window.open(document.getElementById('ReportViewerControl').ClientController.m_exportUrlBase + encodeURIComponent(formatDropDown.value), '_blank') formatDropDown.selectedIndex = 0; document.getElementById('ReportViewerControl_ctl01_ctl05_ctl01').Controller.SetViewerLinkActive(document.getElementById('ReportViewerControl_ctl01_ctl05_ctl00').selectedIndex != 0);return false;" href="#"><script type="text/javascript">
document.getElementById('ReportViewerControl_ctl01_ctl05_ctl01').Controller = new ReportViewerLink("ReportViewerControl_ctl01_ctl05_ctl01", false, "ActiveLink", "DisabledLink", "", "", "");
</script>Export</a></td></tr></tbody></table></div></div></div></td></tr></tbody></table></form></body>
</html>
谢谢!
您需要先实际导航到页面才能加载文档 - 然后使用以下代码:
ie.Document.GetElementByID("ReportViewerControl_ctl01_ctl05_ctl00").Value = "EXCEL"
使用 <option>
标签内的选项。
导航:
ie.Navigate ProductionAddress
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
ie.Document.GetElementByID("ReportViewerControl_ctl01_ctl05_ctl00").Value = "EXCEL"
我真的很难让这段代码起作用,none 我发现的现有问题有明确的答案,所以我想包括我最终使用的有效代码:
Private Sub OKButton_Click()
Dim ProductionAddress As String
ProductionAddress = "http://Collapsed"
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Silent = True
.Visible = True
.Navigate ProductionAddress
End With
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
ie.document.getelementbyid("ReportViewerControl_ctl01_ctl05_ctl00").Value = "EXCEL"
Set objButton = ie.document.getelementbyid("ReportViewerControl_ctl01_ctl05_ctl01")
objButton.Focus
objButton.Click
End Sub
这打开了页面,从下拉列表中选择并成功点击了 ID 元素。
编辑:我清理了源代码格式并更新了我的 VBA 代码。我不再收到错误,但我的浏览器没有任何反应 window(没有 selected,没有点击)。
我有一些 VBA 代码允许用户在 Excel 中的表单中输入信息。该代码获取此信息并生成指向我们内部网上页面的地址。在该页面上有一个 select 导出格式的下拉列表,在选择格式后,用户可以单击以导出文件。
我试过的 VBA 代码(和几个变体)如下:
Public Sub window_Open(strLocation As String, Menubar As Boolean, height As Long, width As Long, resizable As Boolean)
With CreateObject("InternetExplorer.Application")
.Visible = False
.height = height
.width = width
.Menubar = Menubar
.Visible = True
.resizable = True
.Navigate strLocation
End With
End Sub
Private Sub OKButton_Click()
Dim ProductionAddress As String
ProductionAddress = "http://Collapsed"
window_Open ProductionAddress, True, 800, 1000, False
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate ProductionAddress
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
ie.document.getelementbyid("ReportViewerControl_ctl01_ctl05_ctl00").Value = "EXCEL"
Set objButton = ie.document.getelementbyid("ReportViewerControl_ctl01_ctl05_ctl01")
objButton.Focus
objButton.Click
End Sub
我尝试导航的页面的源代码如下。 selecting EXCEL 的元素:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<style>
body, frameset, form {
margin: 0px;
padding: 0px;
}
.PageNumberText, input, button, select, table, body, frameset {
font-size: 8pt;
font-family: Verdana;
}
.ToolbarExport {
display: inline;
}
.WidgetSet {
height: 30px;
}
.ToolBarButtonsCell {
padding-left: 6px;
}
.MenuBarBkGnd {
background-color: #ECE9D8;
}
</style>
</head>
<body style="margin: 0px; overflow: auto;"><form name="ReportViewerForm" id="ReportViewerForm" action="ReportViewer.aspx?%2fProduction%2fWell+Daily+Production&StartDate=01%2f01%2f2015+00%3a00%3a00&EndDate=07%2f21%2f2015+00%3a00%3a00&WellName=Acadia&rs%3aParameterLanguage=&rc%3aParameters=Collapsed" method="post"><table id="ReportViewerControl" style="width: 100%; height: 100%;" onclick="if (document.getElementById('ReportViewerControl_ctl00') != null) document.getElementById('ReportViewerControl_ctl00').ParametersController.HideActiveDropDown();" onactivate="if (document.getElementById('ReportViewerControl_ctl00') != null) document.getElementById('ReportViewerControl_ctl00').ParametersController.HideActiveDropDown();" cellspacing="0" cellpadding="0"><tbody><tr><td><div class="MenuBarBkGnd"><div class="ToolBarButtonsCell"><div class="ToolbarExport WidgetSet"><table style="display: inline;" cellspacing="0" cellpadding="0"><tbody><tr><td height="28"><select name="ReportViewerControl$ctl01$ctl05$ctl00" title="Export Formats" id="ReportViewerControl_ctl01_ctl05_ctl00" onchange="document.getElementById('ReportViewerControl_ctl01_ctl05_ctl01').Controller.SetViewerLinkActive(document.getElementById('ReportViewerControl_ctl01_ctl05_ctl00').selectedIndex != 0);"><option value="EXCEL">Excel</option></select></td></tr></tbody></table></div></div></div></td></tr></tbody></table></form></body>
</html>
以及导出元素的源代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<style>
.DisabledLink {
font-size: 8pt;
font-family: Verdana;
color: gray;
text-decoration: none;
cursor: default;
}
body, frameset, form {
margin: 0px;
padding: 0px;
}
.PageNumberText, input, button, select, table, body, frameset {
font-size: 8pt;
font-family: Verdana;
}
.ToolbarExport {
display: inline;
}
.WidgetSet {
height: 30px;
}
.ToolBarButtonsCell {
padding-left: 6px;
}
.MenuBarBkGnd {
background-color: #ECE9D8;
}
</style>
</head>
<body style="margin: 0px; overflow: auto;"><form name="ReportViewerForm" id="ReportViewerForm" action="ReportViewer.aspx?%2fProduction%2fWell+Daily+Production&StartDate=01%2f01%2f2015+00%3a00%3a00&EndDate=07%2f21%2f2015+00%3a00%3a00&WellName=Acadia&rs%3aParameterLanguage=&rc%3aParameters=Collapsed" method="post"><table id="ReportViewerControl" style="width: 100%; height: 100%;" onclick="if (document.getElementById('ReportViewerControl_ctl00') != null) document.getElementById('ReportViewerControl_ctl00').ParametersController.HideActiveDropDown();" onactivate="if (document.getElementById('ReportViewerControl_ctl00') != null) document.getElementById('ReportViewerControl_ctl00').ParametersController.HideActiveDropDown();" cellspacing="0" cellpadding="0"><tbody><tr><td><div class="MenuBarBkGnd"><div class="ToolBarButtonsCell"><div class="ToolbarExport WidgetSet"><table style="display: inline;" cellspacing="0" cellpadding="0"><tbody><tr><td height="28"><a title="Export" class="DisabledLink" id="ReportViewerControl_ctl01_ctl05_ctl01" onmouseover="this.Controller.OnLinkHover();" onmouseout="this.Controller.OnLinkNormal();" onclick=" var formatDropDown = document.getElementById('ReportViewerControl_ctl01_ctl05_ctl00'); if (formatDropDown.selectedIndex == 0) return false; window.open(document.getElementById('ReportViewerControl').ClientController.m_exportUrlBase + encodeURIComponent(formatDropDown.value), '_blank') formatDropDown.selectedIndex = 0; document.getElementById('ReportViewerControl_ctl01_ctl05_ctl01').Controller.SetViewerLinkActive(document.getElementById('ReportViewerControl_ctl01_ctl05_ctl00').selectedIndex != 0);return false;" href="#"><script type="text/javascript">
document.getElementById('ReportViewerControl_ctl01_ctl05_ctl01').Controller = new ReportViewerLink("ReportViewerControl_ctl01_ctl05_ctl01", false, "ActiveLink", "DisabledLink", "", "", "");
</script>Export</a></td></tr></tbody></table></div></div></div></td></tr></tbody></table></form></body>
</html>
谢谢!
您需要先实际导航到页面才能加载文档 - 然后使用以下代码:
ie.Document.GetElementByID("ReportViewerControl_ctl01_ctl05_ctl00").Value = "EXCEL"
使用 <option>
标签内的选项。
导航:
ie.Navigate ProductionAddress
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
ie.Document.GetElementByID("ReportViewerControl_ctl01_ctl05_ctl00").Value = "EXCEL"
我真的很难让这段代码起作用,none 我发现的现有问题有明确的答案,所以我想包括我最终使用的有效代码:
Private Sub OKButton_Click()
Dim ProductionAddress As String
ProductionAddress = "http://Collapsed"
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Silent = True
.Visible = True
.Navigate ProductionAddress
End With
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
ie.document.getelementbyid("ReportViewerControl_ctl01_ctl05_ctl00").Value = "EXCEL"
Set objButton = ie.document.getelementbyid("ReportViewerControl_ctl01_ctl05_ctl01")
objButton.Focus
objButton.Click
End Sub
这打开了页面,从下拉列表中选择并成功点击了 ID 元素。