VBA从Internet Explorer的下拉菜单中选择 [英] VBA Select from dropdown in Internet Explorer
问题描述
我有一些VBA代码允许用户在Excel中将信息输入到表单中。代码将获取这些信息并生成地址到我们Intranet上的页面。在该页面上有一个下拉列表来选择导出格式,选择格式后用户可以点击导出文件。
我试过的VBA代码(和如下所示:
Public Sub window_Open(strLocation As String,Menubar As Boolean,height As Long,width As Long, resizable As Boolean)
使用CreateObject(InternetExplorer.Application)
.Visible = False
.height = height
.width = width
。 Menubar = Menubar
.Visible = True
.resizable = True
。导航strLocation
End With
End Sub
Private Sub OKButton_Click()
Dim ProductionAddress As String
ProductionAddress =http://折叠
window_Open ProductionAddress,True,800,1000,False
Dim ie As Object
Set ie = CreateObject(InternetExplorer.Application)
ie.Navigate ProductionAddress
虽然ie.ReadyState<> 4或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
我试图导航的页面的源代码如下所示。
用于选择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 =ReportViewerFormid =ReportViewerFormaction =ReportViewer.aspx?%2fProduction%2fWell + Daily + Production&安培;起始日期= 01%2f01%2f2015 + 00%3A00%3A00&放大器;放大器;结束日期= 07%2f21%2f2015 + 00%3A00%3A00&放大器;放大器; WellName =阿卡迪亚&放大器;放大器; RS%3aParameterLanguage =&放大器;放大器; RC% 3aParameters =折叠的method =post>< table id =ReportViewerControlstyle =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 =0cellpadding =0>< tbody>< tr>< td>< div class =MenuBarBkGnd>< div class =ToolBarButtonsCell>< div class = ToolbarExport WidgetSet>< table style =display:inline; cellspacing =0cellpadding =0>< tbody>< tr>< td height =28>< select name =ReportViewerControl $ ctl01 $ ctl05 $ ctl00title =Export Formats ReportViewerControl_ctl01_ctl05_ctl00onchange =document.getElementById('ReportViewerControl_ctl01_ctl05_ctl01')。Controller.SetViewerLinkActive(document.getElementById('ReportViewerControl_ctl01_ctl05_ctl00')。selectedIndex!= 0);>< option value =EXCEL> Excel< ; /选项>< /选择>< / TD>< / TR>< / tbody的>< /表>< / DIV>< / DIV>< / DIV>< / TD>< ; / TR>< / tbody的>< /表>< /形式>< /体>
< / html>
以及导出元素的源代码:
<!DOCTYPE html PUBLIC - // W3C // DTD HTML 4.01 Transitional // EN>
< html>
< head>
< title>< / title>
< style>
.DisabledLink {
font-size:8pt;
font-family:Verdana;
颜色:灰色;
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 =ReportViewerFormid =ReportViewerFormaction =ReportViewer.aspx?%2fProduction%2fWell + Daily + Production&安培;起始日期= 01%2f01%2f2015 + 00%3A00%3A00&放大器;放大器;结束日期= 07%2f21%2f2015 + 00%3A00%3A00&放大器;放大器; WellName =阿卡迪亚&放大器;放大器; RS%3aParameterLanguage =&放大器;放大器; RC% 3aParameters =折叠的method =post>< table id =ReportViewerControlstyle =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 =0cellpadding =0>< tbody>< tr>< td>< div class =MenuBarBkGnd>< div class =ToolBarButtonsCell>< div class = ToolbarExport WidgetSet>< table style =display:inline; cellspacing =0cellpadding =0>< tbody>< tr>< td height =28>< a title =Exportclass =DisabledLinkid =ReportViewerControl_ctl01_ctl05_ctl01onmouseover = this.Controller.OnLinkHover();的onmouseout = this.Controller.OnLinkNormal(); (formatDropDown.selectedIndex == 0)&#10; return false;&#10; window.open (document.getElementById('ReportViewerControl')。ClientController.m_exportUrlBase + encodeURIComponent(formatDropDown.value),'_blank')&#10; formatDropDown.selectedIndex = 0;&#10; 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>导出< / a>< / td>< / tr>< / tbody>< / table>< / div>< / div>< / div>< ; / TD>< / TR>< / tbody的>< /表>< /形式>< /体>
< / html>
谢谢!
ie.Document.GetElementByID(ReportViewerControl_ctl01_ctl05_ctl00)。Value =EXCEL
使用选项即位于< option>
标记内。
导航:
ie.Navigate ProductionAddress
虽然ie.ReadyState<> 4或ie.Busy:DoEvents:Wend
ie.Document.GetElementByID(ReportViewerControl_ctl01_ctl05_ctl00)。Value =EXCEL
edit: I cleaned up the source code formatting and updated my VBA code. I'm no longer getting an error, but nothing is happening in my browser window (nothing selected, nothing clicked).
I have some VBA code that allows a user to enter information into a form in Excel. The code takes this information and generates an address to a page on our intranet. On that page there is a dropdown list to select export format and after a format is chosen the user can click to export the file.
The VBA code I have tried (and several variations) is below:
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
The source code for the page I am attempting to navigate is below. Element for 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>
And the source code for the export element:
<!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>
Thanks!
You need to actually navigate to the page first in order to load the document - then use the below code:
ie.Document.GetElementByID("ReportViewerControl_ctl01_ctl05_ctl00").Value = "EXCEL"
Use the option that is inside the <option>
tag.
To navigate:
ie.Navigate ProductionAddress
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
ie.Document.GetElementByID("ReportViewerControl_ctl01_ctl05_ctl00").Value = "EXCEL"
这篇关于VBA从Internet Explorer的下拉菜单中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!