VBA 自动化 - 如何访问日期选择器元素
VBA Automation - How To Access Date Picker Elements
我正在研究一些 VBA 自动化,它从 Excel 电子表格中获取数据,并将该数据输入到 Intranet 站点。
在 Intranet 站点上有一个小的日期选择器工具(带有开始日期和结束日期),允许您选择日期(请参阅 html 日期选择器 html 代码的摘录) :
<input type="text" id="startdate" name="startdate" value="" class="form-control" size="30">
<button class="pika-button pika-day" type="button" data-pika-year="2019" data-pika-month="8" data-pika-day="2">2</button>
<button class="pika-prev" type="button">Previous Month</button>
<input type="text" id="enddate" name="enddate" value="" class="form-control" size="30">
<button class="pika-button pika-day" type="button" data-pika-year="2019" data-pika-month="8" data-pika-day="2">2</button>
<button class="pika-prev" type="button">Previous Month</button>
另请参阅下面的 JavaScript 日期选择器函数代码:
<script>
var startDate,
endDate,
updateStartDate = function() {
startPicker.setStartRange(startDate);
endPicker.setStartRange(startDate);
endPicker.setMinDate(startDate);
},
updateEndDate = function() {
startPicker.setEndRange(endDate);
startPicker.setMaxDate(endDate);
endPicker.setEndRange(endDate);
},
startPicker = new Pikaday({
field: document.getElementById('startdate'),
minDate: new Date(2016, 1, 1),
maxDate: new Date(2050, 12, 31),
onSelect: function() {
startDate = this.getDate();
updateStartDate();
}
}),
endPicker = new Pikaday({
field: document.getElementById('enddate'),
minDate: new Date(2016, 1, 1),
maxDate: new Date(2050, 12, 31),
onSelect: function() {
endDate = this.getDate();
updateEndDate();
}
});
_startDate = startPicker.getDate();
_endDate = endPicker.getDate();
if (_startDate) {
startDate = _startDate;
updateStartDate();
};
if (_endDate) {
endDate = _endDate;
updateEndDate();
};
</script>
我最初有这段代码将正确的日期字符串推送到表单,但是在 运行 自动化之后,日期选择器中的日期选择器元素没有被选中(它们默认为今天的日期)(参见下面的代码:).
IE.document.all("startdate").Value = Format$(ThisWorkbook.Sheets("Legends").Range("b7").Value, "yyyy-mm-dd")
IE.document.all("enddate").Value = Format$(ThisWorkbook.Sheets("Legends").Range("b8").Value, "yyyy-mm-dd")
这是我 运行 自动化后发生的情况,请查看带有推送日期的图片:Intranet site with date
这是我单击日期选择器元素时的样子:Datepicker defaulting to today's date
在网上看了一些东西后,似乎你必须点击 "startdate" 元素,然后点击日期选择器中的其他元素(我在下面成功完成了:)。
IE.document.querySelector("startdate").Click
但是我无法弄清楚如何点击其他元素(上个月和当天)来完成这部分自动化。
如有任何帮助,我们将不胜感激!
您可以尝试使用SendKeys方法输入日期,请参考以下代码:
Sub Test()
Dim IE As Object
Dim startDateText As Object, endDateText As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate "<the website url>"
While IE.ReadyState <> 4
DoEvents
Wend
'find the startdate textbox and focus
Set startDateText = IE.document.all("startdate")
startDateText.Focus
'using sendkeys method to enter the date.
SendKeys Format$(ThisWorkbook.Sheets("Legends").Range("b7").Value, "yyyy-mm-dd")
'Waiting the date value enters (1 second)
Application.Wait (Now + TimeValue("0:00:1"))
Set endDateText = IE.document.all("enddate")
endDateText.Focus
SendKeys Format$(ThisWorkbook.Sheets("Legends").Range("b8").Value, "yyyy-mm-dd")
Application.Wait (Now + TimeValue("0:00:1"))
IE.document.all("startdate").Click
End With
Set IE = Nothing
End Sub
我正在研究一些 VBA 自动化,它从 Excel 电子表格中获取数据,并将该数据输入到 Intranet 站点。
在 Intranet 站点上有一个小的日期选择器工具(带有开始日期和结束日期),允许您选择日期(请参阅 html 日期选择器 html 代码的摘录) :
<input type="text" id="startdate" name="startdate" value="" class="form-control" size="30">
<button class="pika-button pika-day" type="button" data-pika-year="2019" data-pika-month="8" data-pika-day="2">2</button>
<button class="pika-prev" type="button">Previous Month</button>
<input type="text" id="enddate" name="enddate" value="" class="form-control" size="30">
<button class="pika-button pika-day" type="button" data-pika-year="2019" data-pika-month="8" data-pika-day="2">2</button>
<button class="pika-prev" type="button">Previous Month</button>
另请参阅下面的 JavaScript 日期选择器函数代码:
<script>
var startDate,
endDate,
updateStartDate = function() {
startPicker.setStartRange(startDate);
endPicker.setStartRange(startDate);
endPicker.setMinDate(startDate);
},
updateEndDate = function() {
startPicker.setEndRange(endDate);
startPicker.setMaxDate(endDate);
endPicker.setEndRange(endDate);
},
startPicker = new Pikaday({
field: document.getElementById('startdate'),
minDate: new Date(2016, 1, 1),
maxDate: new Date(2050, 12, 31),
onSelect: function() {
startDate = this.getDate();
updateStartDate();
}
}),
endPicker = new Pikaday({
field: document.getElementById('enddate'),
minDate: new Date(2016, 1, 1),
maxDate: new Date(2050, 12, 31),
onSelect: function() {
endDate = this.getDate();
updateEndDate();
}
});
_startDate = startPicker.getDate();
_endDate = endPicker.getDate();
if (_startDate) {
startDate = _startDate;
updateStartDate();
};
if (_endDate) {
endDate = _endDate;
updateEndDate();
};
</script>
我最初有这段代码将正确的日期字符串推送到表单,但是在 运行 自动化之后,日期选择器中的日期选择器元素没有被选中(它们默认为今天的日期)(参见下面的代码:).
IE.document.all("startdate").Value = Format$(ThisWorkbook.Sheets("Legends").Range("b7").Value, "yyyy-mm-dd")
IE.document.all("enddate").Value = Format$(ThisWorkbook.Sheets("Legends").Range("b8").Value, "yyyy-mm-dd")
这是我 运行 自动化后发生的情况,请查看带有推送日期的图片:Intranet site with date
这是我单击日期选择器元素时的样子:Datepicker defaulting to today's date
在网上看了一些东西后,似乎你必须点击 "startdate" 元素,然后点击日期选择器中的其他元素(我在下面成功完成了:)。
IE.document.querySelector("startdate").Click
但是我无法弄清楚如何点击其他元素(上个月和当天)来完成这部分自动化。
如有任何帮助,我们将不胜感激!
您可以尝试使用SendKeys方法输入日期,请参考以下代码:
Sub Test()
Dim IE As Object
Dim startDateText As Object, endDateText As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate "<the website url>"
While IE.ReadyState <> 4
DoEvents
Wend
'find the startdate textbox and focus
Set startDateText = IE.document.all("startdate")
startDateText.Focus
'using sendkeys method to enter the date.
SendKeys Format$(ThisWorkbook.Sheets("Legends").Range("b7").Value, "yyyy-mm-dd")
'Waiting the date value enters (1 second)
Application.Wait (Now + TimeValue("0:00:1"))
Set endDateText = IE.document.all("enddate")
endDateText.Focus
SendKeys Format$(ThisWorkbook.Sheets("Legends").Range("b8").Value, "yyyy-mm-dd")
Application.Wait (Now + TimeValue("0:00:1"))
IE.document.all("startdate").Click
End With
Set IE = Nothing
End Sub