Excel VBA - 无法触发 ng-change 事件
Excel VBA - Unable to fire ng-change event
我是 VBA 的新手,我正在尝试使某些东西自动化,但卡在了 fireEvent
部分。
基本上我想触发下拉框的事件,但我不知道该怎么做。
下面的代码选择了我想要的选项,但无法触发新的页面事件并以
结束
"Run time error: 5 Invalid procedure call or argument"
拜托,如果有人能帮我找到解决办法,我将不胜感激。
我的ExcelVBA代码:
Sub Auto2()
Dim ie As New InternetExplorer
With ie
.Visible = True
.navigate ""
While .Busy Or .readyState < 4: DoEvents: Wend
ie.document.getElementsByClassName("input-group-addon")(0).Click
ie.document.getElementsByClassName("today day")(0).Click
While .Busy Or .readyState < 4: DoEvents: Wend
ie.document.querySelector("select[name='tipoRecibo']").Value = "string:R"
ie.document.querySelector("select[name='tipoRecibo']").fireEvent "ng-change"
End With
End Sub
网页代码:
<div class="col-md-4 col-xs-12">
<lf-dropdown
class="ng-isolate-scope"
lf-model="$ctrl.formParameters.tipoRecibo"
lf-label="Tipo"
name="tipoRecibo"
lf-empty-option="true"
lf-disabled="$ctrl.isTipoDisabled()"
lf-change="$ctrl.pfChange(model)"
lf-options="$ctrl.getTipos()"
>
<div class="form-group form-group-sm">
<label title="" class="ng-binding" ng-attr-data-toggle="{{$ctrl.lfHelp ? 'tooltip' : undefined }}"> Tipo<!-- ngIf: $ctrl.lfHelp --> </label>
<select
name="tipoRecibo"
class="form-control ng-pristine ng-untouched ng-valid ng-empty"
ng-disabled="$ctrl.lfDisabled({model: $ctrl.lfModel})"
ng-model="$ctrl.lfModel"
ng-init="$ctrl.lfOptions\[0\]"
ng-options="option.value as option.text for option in $ctrl.lfOptions"
ng-change="$ctrl.lfChange({model: $ctrl.lfModel})"
>
<!-- ngIf: $ctrl.lfEmptyOption -->
<option class="ng-pristine ng-untouched ng-valid ng-scope ng-empty" value="" ng-if="$ctrl.lfEmptyOption" ng-model="$ctrl.lfModel"></option>
<!-- end ngIf: $ctrl.lfEmptyOption -->
<option value="string:R" label="Fatura-Recibo">Fatura-Recibo</option>
<option value="string:FTR" label="Fatura">Fatura</option>
</select>
<div class="opcional-label ng-binding"></div>
<span class="help-block"></span>
</div>
</lf-dropdown>
</div>
我认为事件不是 ng-change
我认为它只是 change
。 Fireevent
今天在大多数情况下不起作用。正如 QHaar 所写,尝试附加和分派事件。
您可以在这里查看更多信息,了解如何检查哪些事件是可行的:
尝试以下操作:
Sub Auto2()
Dim ie As New InternetExplorer
Dim nodeDropdown As Object
ie.Visible = True
ie.navigate ""
While .Busy Or ie.readyState < 4: DoEvents: Wend
ie.document.getElementsByClassName("input-group-addon")(0).Click
ie.document.getElementsByClassName("today day")(0).Click
While .Busy Or .readyState < 4: DoEvents: Wend
Set nodeDropdown = ie.document.querySelector("select[name='tipoRecibo']")
nodeDropdown.Value = "string:R"
Call TriggerEvent(ie.document, nodeDropdown.Value, "change")
End Sub
而这个程序触发的事件:
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
Dim theEvent As Object
htmlElementWithEvent.Focus
Set theEvent = htmlDocument.createEvent("HTMLEvents")
theEvent.initEvent eventType, True, False
htmlElementWithEvent.dispatchEvent theEvent
End Sub
我是 VBA 的新手,我正在尝试使某些东西自动化,但卡在了 fireEvent
部分。
基本上我想触发下拉框的事件,但我不知道该怎么做。
下面的代码选择了我想要的选项,但无法触发新的页面事件并以
结束"Run time error: 5 Invalid procedure call or argument"
拜托,如果有人能帮我找到解决办法,我将不胜感激。
我的ExcelVBA代码:
Sub Auto2()
Dim ie As New InternetExplorer
With ie
.Visible = True
.navigate ""
While .Busy Or .readyState < 4: DoEvents: Wend
ie.document.getElementsByClassName("input-group-addon")(0).Click
ie.document.getElementsByClassName("today day")(0).Click
While .Busy Or .readyState < 4: DoEvents: Wend
ie.document.querySelector("select[name='tipoRecibo']").Value = "string:R"
ie.document.querySelector("select[name='tipoRecibo']").fireEvent "ng-change"
End With
End Sub
网页代码:
<div class="col-md-4 col-xs-12">
<lf-dropdown
class="ng-isolate-scope"
lf-model="$ctrl.formParameters.tipoRecibo"
lf-label="Tipo"
name="tipoRecibo"
lf-empty-option="true"
lf-disabled="$ctrl.isTipoDisabled()"
lf-change="$ctrl.pfChange(model)"
lf-options="$ctrl.getTipos()"
>
<div class="form-group form-group-sm">
<label title="" class="ng-binding" ng-attr-data-toggle="{{$ctrl.lfHelp ? 'tooltip' : undefined }}"> Tipo<!-- ngIf: $ctrl.lfHelp --> </label>
<select
name="tipoRecibo"
class="form-control ng-pristine ng-untouched ng-valid ng-empty"
ng-disabled="$ctrl.lfDisabled({model: $ctrl.lfModel})"
ng-model="$ctrl.lfModel"
ng-init="$ctrl.lfOptions\[0\]"
ng-options="option.value as option.text for option in $ctrl.lfOptions"
ng-change="$ctrl.lfChange({model: $ctrl.lfModel})"
>
<!-- ngIf: $ctrl.lfEmptyOption -->
<option class="ng-pristine ng-untouched ng-valid ng-scope ng-empty" value="" ng-if="$ctrl.lfEmptyOption" ng-model="$ctrl.lfModel"></option>
<!-- end ngIf: $ctrl.lfEmptyOption -->
<option value="string:R" label="Fatura-Recibo">Fatura-Recibo</option>
<option value="string:FTR" label="Fatura">Fatura</option>
</select>
<div class="opcional-label ng-binding"></div>
<span class="help-block"></span>
</div>
</lf-dropdown>
</div>
我认为事件不是 ng-change
我认为它只是 change
。 Fireevent
今天在大多数情况下不起作用。正如 QHaar 所写,尝试附加和分派事件。
您可以在这里查看更多信息,了解如何检查哪些事件是可行的:
尝试以下操作:
Sub Auto2()
Dim ie As New InternetExplorer
Dim nodeDropdown As Object
ie.Visible = True
ie.navigate ""
While .Busy Or ie.readyState < 4: DoEvents: Wend
ie.document.getElementsByClassName("input-group-addon")(0).Click
ie.document.getElementsByClassName("today day")(0).Click
While .Busy Or .readyState < 4: DoEvents: Wend
Set nodeDropdown = ie.document.querySelector("select[name='tipoRecibo']")
nodeDropdown.Value = "string:R"
Call TriggerEvent(ie.document, nodeDropdown.Value, "change")
End Sub
而这个程序触发的事件:
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
Dim theEvent As Object
htmlElementWithEvent.Focus
Set theEvent = htmlDocument.createEvent("HTMLEvents")
theEvent.initEvent eventType, True, False
htmlElementWithEvent.dispatchEvent theEvent
End Sub