使用VBA将字符串从UserForm传递到网站字段

使用VBA将字符串从UserForm传递到网站字段,vba,excel,internet-explorer,Vba,Excel,Internet Explorer,我对使用VBA进行web交互很陌生 我有一个带有文本框(“TxtSql”)的用户表单(“FrmResults”)。我正在尝试使用VBA打开网站,并将文本框中的文本粘贴到此网站上的文本区域 到目前为止,我有以下代码,但这总是在最后一部分启动调试器,即我试图设置textarea的值的地方-即使我只是使用“test”进行测试。我还尝试使用.InnerHtml而不是.Value,但也失败了 它总是在打开网站后停止,因此无法粘贴从剪贴板复制的文本。 Dim IE As Object Dim

我对使用VBA进行web交互很陌生

我有一个带有文本框(“
TxtSql
”)的用户表单(“
FrmResults
”)。我正在尝试使用VBA打开网站,并将文本框中的文本粘贴到此网站上的文本区域

到目前为止,我有以下代码,但这总是在最后一部分启动调试器,即我试图设置textarea的值的地方-即使我只是使用“test”进行测试。我还尝试使用
.InnerHtml
而不是
.Value
,但也失败了

它总是在打开网站后停止,因此无法粘贴从剪贴板复制的文本。

    Dim IE As Object
    Dim MyURL As String
    Dim varResults As New DataObject

    varResults.SetText TxtSql.Text
    varResults.PutInClipboard

    Set IE = CreateObject("InternetExplorer.Application")
    MyURL = "myURL"

    IE.Navigate MyURL
    IE.Visible = True

    While IE.busy
    DoEvents

    Wend

    With IE.Document
        .getElementById("Oracle_profile_sql").Value = varResults
'        .all("mapnow_button").Click
    End With
有人能告诉我我做错了什么或错过了什么吗

我的代码:

    Dim IE As Object
    Dim MyURL As String
    Dim varResults As New DataObject

    varResults.SetText TxtSql.Text
    varResults.PutInClipboard

    Set IE = CreateObject("InternetExplorer.Application")
    MyURL = "myURL"

    IE.Navigate MyURL
    IE.Visible = True

    While IE.busy
    DoEvents

    Wend

    With IE.Document
        .getElementById("Oracle_profile_sql").Value = varResults
'        .all("mapnow_button").Click
    End With

取决于您尝试访问的站点。 例如,元素可以是数组。以下是一些有效的方法:

Option Explicit
Public Sub TestMe()

    Dim IE          As Object
    Dim MyURL       As String
    Dim varResults  As New DataObject
    Dim el          As Object

    Set IE = CreateObject("InternetExplorer.Application")
    MyURL = "http://www.stackoverflow.com"

    IE.Navigate MyURL
    IE.Visible = True

    While IE.busy
        Application.Wait (Now + TimeValue("0:00:2"))
    Wend


    Set el = IE.Document.getElementById("search")(0)
    el.value = "TEST"

End Sub
下面的代码将打开“Google.com”(用于我的测试),并将“TextSql”
TextBox
(来自用户表单)中的值放入“谷歌”网站的“搜索”文本栏中

在您的情况下,请使用以下行:
.getElementById(“Oracle_profile_sql”).Value=varResults.GetText(1)

代码

Private Sub CommandButton1_Click()

    Dim IE As Object
    Dim MyURL As String
    Dim varResults As New DataObject

    varResults.SetText TxtSql.Text
    varResults.PutInClipboard

    Set IE = CreateObject("InternetExplorer.Application")

    ' tested with the "Search" in Google
    MyURL = "https://www.google.de/?gfe_rd=cr&ei=pj2TWLrWFYTN8gf0uIaIBw&gws_rd=ssl" ' "myURL"

    IE.Navigate MyURL
    IE.Visible = True

    While IE.busy
        DoEvents
    Wend

    With IE.Document
        .getElementById("lst-ib").Value = varResults.GetText(1)
    End With

End Sub

非常感谢!如果我使用您的代码,那么调试器将跳到底部的第二行。元素是div中的一个普通文本区域。还有其他想法吗?@Mike-再试一次。嗨,Shai,非常感谢-这太棒了!.GetText(1)成功了。:)