【问题标题】:Using VBA to manipulate IE with submitting form (trouble with textarea field)使用 VBA 通过提交表单操作 IE(textarea 字段出现问题)
【发布时间】:2016-11-21 05:41:13
【问题描述】:

你好 StackOverflow 社区,

我有一个关于使用 VBA 操作 IE 的问题。

代码是关于以网站的形式插入数据。到目前为止,一切都很顺利,除了以下“textarea”字段:

<textarea cols="1" rows="1" id="id_content" name="name_content" class="htmleditor">Content of the textarea is displayed here</textarea>

除了实际字段(来自上面提到的 textarea 类型)之外,还有一些来自 type button 的元素,它们包含在 htmleditor 中。

我想要做的是更改文本字段中的文本。因此,我希望能够写一些不同的东西,而不是“此处显示 textarea 的内容”。如果可能的话,也改变格式,例如有些部分加粗等。然后提交表单。

我目前使用的代码:

Dim IEApp As SHDocVw.InternetExplorer
Set IEApp = New SHDocVw.InternetExplorer

Dim htmlDocument As htmlDocument
Dim objNotes As Object
Dim strNotes As String

With IEApp
    IEApp.Visible = True
    IEApp.Navigate "..."
    Do: Loop Until IEApp.Busy = False
    Do: Loop Until IEApp.document.ReadyState = "complete"
End With

Set objNotes = IEApp.document.getElementsByName("name_content")(0)

MsgBox objNotes.innerText ' 1)
MsgBox objNotes.innerHTML ' 2)
MsgBox objNotes.outerHTML ' 3)
MsgBox objNotes.Value ' 4)

objNotes.Value = "Hello World" 'Here I want to change the text in the textfield

MsgBox objNotes.innerText ' 5)
MsgBox objNotes.innerHTML ' 6)
MsgBox objNotes.outerHTML ' 7)
MsgBox objNotes.Value ' 8)

IEApp.document.forms(0).submit

MsgBox 命令给出以下内容

1) <p>Content of the textarea is displayed here</p>
2) &lt;p&gt;Content of the textarea is displayed here&lt;/p&gt;
3) <textarea name="name_content" class="htmleditor" id="id_content" aria-hidden="true" style="display: none;" rows="1" cols="1">&lt;p&gt;Content of the textarea is displayed here&lt;/p&gt;</textarea>
4) <p>Content of the textarea is displayed here</p>

5) <p>Hello World</p>
6) &lt;p&gt;Hello World&lt;/p&gt;
7) <textarea name="name_content" class="htmleditor" id="id_content" aria-hidden="true" style="display: none;" rows="1" cols="1">&lt;p&gt;Hello World&lt;/p&gt;</textarea>
8) <p>Hello World</p>

但是在成功提交表单后页面并没有改变。

到目前为止我尝试了什么:

  • getElementByID 代替 getElementsbyName
  • 通过浏览网站的所有元素并直接引用它来搜索此元素
  • 改变innerText、innerHTML、outerHTML、value的值
  • 因为 aria-Hidden = True 试图通过 VBA 更改它,但我找不到合适的方法
  • 定义一个新的 textArea 变量为 Object / HTMLTextAreaElement / HTMLRichtextElement / HTMLInputTextElement / HTMLAreaElement 并通过它更改值,但没有任何数据类型影响它
  • 将公式设置为活动,将其聚焦但无法使用 SendKeys 函数以这种方式编写文本

知道还能做什么吗?

【问题讨论】:

  • 看看之前的失败,我认为你是在正确的轨道上。确保首先赋予该元素焦点,然后更新元素的 value 属性。此外,您可能需要触发 OnChange 事件。触发事件: IE.Document.getElementById("MyID").FireEvent ("OnChange")
  • 之前和现在都试过了,但它仍然没有显示也没有改变。
  • 可以分享一下网站吗?
  • 遗憾的是,它是一个内部页面。否则我会在之前提到它
  • 请记住,您可能需要在触发 OnChange 事件后赋予不同的元素焦点,否则它可能不会触发。请参阅:w3schools.com/tags/ev_onchange.asp。如果无法访问该站点,我不确定我还能获得多少帮助。不过我不会使用 sendkeys...太多的噩梦,哈哈。

标签: html excel forms vba internet-explorer


【解决方案1】:

对于 TextArea 不使用 byid 或 byname,而是使用 byTagName 我相信你必须使用函数 getElementsbyTagName

【讨论】:

    猜你喜欢
    • 2012-08-02
    • 1970-01-01
    • 2013-04-22
    • 2012-08-17
    • 2017-07-31
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多