VBA Internet Explorer:无法填充WebForm

时间:2017-04-26 16:04:59

标签: vba internet-explorer access-vba

我有以下代码来填充webform,但在这个特定的网站中,代码无效:

Const loginSelectSistema_Name = "url"
Const loginInputUsuário_Name = "Usuario"
Const loginInputSenha_Name = "Senha"
Const loginInputOk_Name = "imageField2"

Const USERNAME = "NomeUsuário"
Const PASSWORD = "SenhaAcesso"

Dim ieApp As InternetExplorer
Dim iePage As HTMLDocument

Function loginPage()
    '
    Set ieApp = New InternetExplorer
    '
    ieApp.Visible = True
    ieApp.navigate (loginWebsite)
    '
    'Wait for page to load
    Do Until ieApp.ReadyState = READYSTATE_COMPLETE
    Loop
    '
    '
    Set iePage = ieApp.Document
    '
    fillInputObject loginSelectSistema_Name, "jcaa"
    fillInputObject loginInputUsuário_Name, USERNAME
    fillInputObject loginInputSenha_Name, PASSWORD
    '
    iePage.all.Item(loginInputOk_Name).Click
End Function

Private Sub fillInputObject(ByVal objectName As String, ByVal value As String, Optional IdentificadorElemento As IdentificadorElementoHTML = Nome)
'
'Preenche o Objeto Input com um determinado valor
    Dim elementHTML As HTMLObjectElement
    '
    If (IdentificadorElemento = Nome) Then
        Set elementHTML = iePage.getElementsByName(objectName).Item
    End If
    '
    If (IdentificadorElemento = Id) Then
        Set elementHTML = iePage.getElementById(objectName)
    End If
    '
    If (IsObject(elementHTML)) Then
        If (Not (elementHTML Is Nothing)) Then
            elementHTML.value = value
        End If
    End If
End Sub

调试时,在功能fillInputObject中,行If (Not (elementHTML Is Nothing)) Then的值始终为 Nothing ,并且未填充网络表单。

以下是我尝试填充网络表单的HTML页面:

<table width="90%" cellspacing="0" cellpadding="0" bordercolor="#DADADA" border="1" bgcolor="#F7F7F8" align="center" height="110">
    <tbody><tr height="2"><td width="62"></td><td width="0"></td><td width="592"></td><td width="223"></td></tr>
          <tr height="15">
             <td colspan="4" bordercolor="#F7F7F8" style="vertical-align:left"><strong>Selecione o Sistema:</strong></td>
          </tr>
          <tr height="15">
             <td colspan="4" bordercolor="#F7F7F8" style="vertical-align:left">
                <font size="1px" face="Verdana, Arial, Helvetica, sans-serif">
               <select name="url" class="combobox" style="width:180px;">

                 <option value="jcaa">Cadastro de Alunos</option>
                 <option value="paef">Cadastro Funcional</option>
                 <option value="paec">Controle de Freqüência</option>
                 <option value="papc">Desp de Pessoal (SDPE)</option>

                 <option value="jcgo">Cargos</option>
                 <option value="paea">Contagem de Tempo</option>
                 <option value="jata">Formação Curricular</option>
                 <option value="jrha">Gratificação / Promoção QM</option>
                 <option value="paex">PAEX - Cadastro Funcional</option>
               </select>
             </font>
             </td>
          </tr>
            <tr height="2"><td colspan="4" bordercolor="#F7F7F8"></td></tr>
            <tr><td colspan="2" bordercolor="#F7F7F8" align="left">
                    <font size="1" face="Verdana, Arial, Helvetica, sans-serif">
                        Usuário
                    </font>
                </td>
                <td colspan="2" bordercolor="#F7F7F8" align="left">
                    <input id="Usuario" name="Usuario" size="10" maxlength="10" value="SE0452336A" type="text">
                </td></tr>
            <tr><td colspan="2" bordercolor="#F7F7F8" align="left">
                    <font size="1" face="Verdana, Arial, Helvetica, sans-serif">
                    Senha
                    </font>
                </td>
                <td colspan="2" bordercolor="#F7F7F8" align="left">
                    <input id="Senha" name="Senha" size="14" maxlength="14" type="password">
                </td></tr>
            <tr>
                <td colspan="2" bordercolor="#F7F7F8" align="left">
                    <font size="1" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;

                    </font>
                </td>
                <td colspan="2" bordercolor="#F7F7F8" align="left">
                    <input name="imageField2" src="images/ok_sen.gif" width="24" type="image" border="0" height="17">
                </td></tr>

        </tbody></table>

我做错了什么?

1 个答案:

答案 0 :(得分:1)

上一条评论中提供的网址显示了问题。登录表单放在IFrame内,因此当前文档不知道元素,因为它们不在那里。

您应该稍微修改一下代码,以便IFrame的文档用于搜索。

Set iePage = ieApp.document

Dim fr As HTMLIFrame
Set fr = iePage.frames("aplicativos")

Dim iframeDoc As HTMLDocument
Set iframeDoc = iePage.frames("aplicativos").document

fillInputObject iframeDoc, loginSelectSistema_Name, "jcaa"
fillInputObject iframeDoc, loginInputUsuário_Name, USERNAME
fillInputObject iframeDoc, loginInputSenha_Name, PASSWORD

iframeDoc.all.Item(loginInputOk_Name).Click

修改函数fillInputObject,使其使用正确的Document对象。

Private Sub fillInputObject(doc As HTMLDocument, ByVal objectName As String, ByVal Value As String, Optional IdentificadorElemento As IdentificadorElementoHTML = Nome)
'
'Preenche o Objeto Input com um determinado valor
    Dim elementHTML As HTMLObjectElement
    '
    If (IdentificadorElemento = Nome) Then
        Set elementHTML = doc.getElementsByName(objectName).Item
    End If
    '
    If (IdentificadorElemento = ID) Then
        Set elementHTML = doc.getElementById(objectName)
    End If
    '
    If (IsObject(elementHTML)) Then
        If (Not (elementHTML Is Nothing)) Then
            elementHTML.Value = Value
        End If
    End If
End Sub

enter image description here