使用Excel VBA填写并提交Google文档表单

使用Excel VBA填写并提交Google文档表单,vba,excel,add-in,online-forms,Vba,Excel,Add In,Online Forms,我正在尝试使用Excel VBA执行类似于但的操作。我想在每次按下Excel加载项上的按钮时,在google文档表单上提交一个响应。AdIn将是XLA文件,用VBA编写。 我希望能够收集用户正在使用的功能。如果有人有更好的解决方案,我愿意 ---编辑--- 是我试图写入的表单(其中一个字段的代码摘录) 用户名 * --编辑2-- 这是我到目前为止尝试过的,但仍然不起作用。我在写“.UserName.Value=Environ”(“UserName”)”的行上收到一个错误,我怀疑这是因为它找不

我正在尝试使用Excel VBA执行类似于但的操作。我想在每次按下Excel加载项上的按钮时,在google文档表单上提交一个响应。AdIn将是XLA文件,用VBA编写。 我希望能够收集用户正在使用的功能。如果有人有更好的解决方案,我愿意

---编辑---

是我试图写入的表单(其中一个字段的代码摘录)


用户名
*
--编辑2-- 这是我到目前为止尝试过的,但仍然不起作用。我在写“.UserName.Value=Environ”(“UserName”)”的行上收到一个错误,我怀疑这是因为它找不到项.UserName

Private Sub GoogleForm()
    Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")
    On Error GoTo errHandler
    With ie
        .navigate "http://spreadsheets.google.com/viewform?hl=en&cfg=true&formkey=dHFTMzkwR2RpY2tzSUNnbVhIcDN3WWc6MA"
        Do While .busy: DoEvents:  Loop
            Do While .ReadyState <> 4: DoEvents: Loop
                With .document.Forms(1)
                     'Username
                    .UserName.Value = Environ("username")
                     'Key
                    .Key.Value = "00qwe-12ckd"
                    .submit
                End With
                Do While Not CBool(InStrB(1, .document.URL, _
                    "cp_search_response-e.asp"))
                    DoEvents
                Loop
                Do While .busy: DoEvents: Loop
                Do While .ReadyState <> 4: DoEvents: Loop
                MsgBox .document.all.tags("table").Item(11).Rows(1).Cells(7).innerText
    End With
Exit Sub
errHandler:
    ie.Quit: Set ie = Nothing
End Sub
Private Sub GoogleForm()
模糊的物体
设置ie=CreateObject(“InternetExplorer.Application”)
关于错误转到错误处理程序
与ie
.导航“http://spreadsheets.google.com/viewform?hl=en&cfg=true&formkey=dHFTMzkwR2RpY2tzSUNnbVhIcDN3WWc6MA"
请稍候。忙碌:DoEvents:循环
执行While.ReadyState 4:DoEvents:Loop
附.文件.表格(1)
'用户名
.UserName.Value=Environ(“用户名”)
“钥匙
.Key.Value=“00qwe-12ckd”
提交
以
不要使用CBool(InStrB(1、.document.URL、_
“cp_搜索_响应-e.asp”))
多芬特
环
请稍候。忙碌:DoEvents:循环
执行While.ReadyState 4:DoEvents:Loop
MsgBox.document.all.tags(“表”).项(11).行(1).单元格(7).内部文本
以
出口接头
错误处理程序:
ie.退出:设置ie=Nothing
端接头

要简化此过程,您需要将其分为两个步骤

  • 准确地计算出谷歌文档需要什么帖子。我会用Firebug或类似的方法来解决这个问题。我猜是类似于
    formkey
    ,然后是一组字段,比如
    field1
    field2
    等等

  • 现在使用MSXML2发布数据(我不知道为什么它没有显示为代码格式)

    设置http=CreateObject(“MSXML2.ServerXMLHTTP”)

    myURL=“”

    http.Open“POST”,myURL,False

    http.setRequestHeader“用户代理”、“Mozilla/4.0(兼容;MSIE 6.0;Windows NT 5.0)”

    http.send(“”“”//不确定是否需要此额外的发送。。可能不会

    http.send(“formkey=Fd0SHgwQ3Yw&field1=A&field2=B”)

    MsgBox http.responseText


  • 目前仅适用于拥有谷歌应用程序帐户的用户(通常是公司)。有很多请求a)能够通过VBA访问此应用程序,b)允许非应用程序用户访问-不幸的是,在过去8个月中,这些请求没有重大更新。

    我能找到的最佳解决方案是使用SendKey。我知道这不太理想,但这里没有任何其他反馈,以我有限的知识,这是我能想出的最好办法。我已经接受了这个答案,由于悬赏请求,我无法撤销接受,但如果这里有更好的想法,我会投票并留下评论,说明这就是答案

    Sub FillOutGoogleForm()
        Application.ScreenUpdating = False
        Dim IE As Object
        Dim uname       As String
        Dim ukey        As String
    
        uname = Environ("username")
        ukey = "00000-123kd-34kdkf-slkf"
    
        Set IE = CreateObject("InternetExplorer.Application")
        IE.Visible = True
    
        While IE.busy
            DoEvents
        Wend
    
        IE.navigate "http://spreadsheets.google.com/viewform?hl=en&pli=1&formkey=dHFTMzkwR2RpY2tzSUNnbVhIcDN3WWc6MA"
    
        While IE.busy
            DoEvents
        Wend
    
        SendKeys uname
        While IE.busy
            DoEvents
        Wend
        SendKeys "{TAB}", True
        SendKeys ukey
        While IE.busy
            DoEvents
        Wend
        SendKeys "{TAB}", True
        SendKeys "{ENTER}", True
        SendKeys "%{F4}"
        Application.ScreenUpdating = True
    End Sub
    

    Mark Nold的答案通常是正确的,除了您应该使用WinHTTP而不是ServerXMLHTTP来避免必须设置代理等问题

    还可以适当地设置内容类型标题。这很可能是“application/x-www-form-urlencoded”(此处有更多相关信息:)

    最后,必须在send()调用中使用发送数据

    转到表单编辑器

    从响应中选择预填充url

    填写像a1 a2 a3 a4这样的字段名作为答案,以便稍后查看

    然后将url中的从viewform更改为formResponse,如下所示:

    https://docs.google.com/forms/d/123-ycyAMD4/viewform?entry.1237336855=a1..
    

    然后http以如下方式获取此url:

    Sub sendresult()
    dim a1,a2,a3
    a1="ans1"    
    a2="ans2"
    a3="ans3"
    
    
    dim myURL
    myURL= "https://docs.google.com/forms/d/123-ycyAMD4/formResponse?" & _ 
     "entry.1237336855=" & a1 & _ 
    "&entry.2099352330=" & a2 & _ 
    "&entry.962062701=" & a3
    
    dim http
    Set http= CreateObject("MSXML2.ServerXMLHTTP")
    http.Open "GET", myURL, False
    http.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    http.send  
    MsgBox http.responseText
    
    end sub
    
    我使用的全部功能:

    'http://stackoverflow.com/questions/2360153/use-excel-vba-to-fill-out-and-submit-google-docs-form/28079922#28079922
    
    Dim savedname
    
    Sub sendresult()
    
    
    Dim ScriptEngine
    Set ScriptEngine = CreateObject("MSScriptControl.ScriptControl")
    ScriptEngine.Language = "JScript"
    ScriptEngine.AddCode "function encode(str) {return encodeURIComponent(str);}"
    
    Dim name, points, times, t1, t2, t3, t4
    
    times = Sheet5.Range("C13").Value
    
    If times = "0" Or times = "" Then
    MsgBox "no data"
    Exit Sub
    End If
    
    If savedname = Empty Then savedname = InputBox("enter your name")
    
    name = ScriptEngine.Run("encode", savedname)
    points = Sheet5.Range("C12").Value
    t1 = Sheet5.Range("C7").Value
    t2 = Sheet5.Range("C8").Value
    t3 = Sheet5.Range("C9").Value
    t4 = Sheet5.Range("C10").Value
    
    
    Dim myURL
    myURL = "https://docs.google.com/forms/d/123-ycyAMD4/formResponse?" & _
     "entry.1237336855=" & name & _
    "&entry.2099352330=" & points & _
    "&entry.962062701=" & times & _
    "&entry.1420067848=" & t1 & _
    "&entry.6696464=" & t2 & _
    "&entry.1896090524=" & t3 & _
    "&entry.1172632640=" & t4
    
    
    Dim http
    Set http = CreateObject("MSXML2.ServerXMLHTTP")
    http.Open "GET", myURL, False
    http.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    http.send
    Dim resp
    
    If UBound(Split(http.responseText, "<div class=""ss-resp-message"">")) > 0 Then
     resp = Split(Split(http.responseText, "<div class=""ss-resp-message"">")(1), "</div>")(0)
    Else
     resp = "sent(with unexpected server response)"
    End If
    If resp = "Your response has been recorded." Then resp = "input received"
    MsgBox resp
    
    
    End Sub
    
    'http://stackoverflow.com/questions/2360153/use-excel-vba-to-fill-out-and-submit-google-docs-form/28079922#28079922
    Dim savedname
    子发送结果()
    暗淡脚本引擎
    设置ScriptEngine=CreateObject(“MSScriptControl.ScriptControl”)
    ScriptEngine.Language=“JScript”
    ScriptEngine.AddCode“函数编码(str){return encodeURIComponent(str);}”
    尺寸名称、点、时间、t1、t2、t3、t4
    时间=表5.范围(“C13”).值
    如果times=“0”或times=”“,则
    MsgBox“无数据”
    出口接头
    如果结束
    如果savedname=Empty,则savedname=InputBox(“输入您的姓名”)
    name=ScriptEngine.Run(“encode”,savedname)
    点=表5.范围(“C12”).值
    t1=表5.范围(“C7”).值
    t2=表5.范围(“C8”).值
    t3=表5.范围(“C9”).值
    t4=表5.范围(“C10”).值
    暗淡的myURL
    myURL=”https://docs.google.com/forms/d/123-ycyAMD4/formResponse?" & _
    “entry.1237336855=“&name&_
    “&entry.2099352330=“&points&_
    “&entry.962062701=“×&_
    “&entry.1420067848=“&t1&_
    “&entry.6696464=“&t2&_
    “&entry.1896090524=“&t3&_
    “&entry.1172632640=“&t4
    暗淡的http
    设置http=CreateObject(“MSXML2.ServerXMLHTTP”)
    http.Open“GET”,myURL,False
    http.setRequestHeader“用户代理”、“Mozilla/4.0(兼容;MSIE 6.0;Windows NT 5.0)”
    http.send
    暗响应
    如果UBound(拆分(http.responseText,“”)>0,则
    resp=Split(Split(http.responseText,“”)(1),“”)(0)
    其他的
    resp=“已发送(带有意外的服务器响应)”
    如果结束
    如果resp=“已记录您的响应。”则resp=“已收到输入”
    MsgBox resp
    端接头
    
    @guitarthrower:我只是想看看下面的答案是否回答了您的问题。抱歉耽搁了。我没有忘记。我在其他项目上遇到了一些积压工作。今晚我应该有时间看一下。你能看一下吗?@御宅:我根据我的尝试编辑了这个问题。如果您有任何进一步的输入,请告诉我。我如何使用您的代码写入字段?我已将我的原始问题编辑为
    https://docs.google.com/forms/d/123-ycyAMD4/formResponse?entry.1237336855=a1...
    
    Sub sendresult()
    dim a1,a2,a3
    a1="ans1"    
    a2="ans2"
    a3="ans3"
    
    
    dim myURL
    myURL= "https://docs.google.com/forms/d/123-ycyAMD4/formResponse?" & _ 
     "entry.1237336855=" & a1 & _ 
    "&entry.2099352330=" & a2 & _ 
    "&entry.962062701=" & a3
    
    dim http
    Set http= CreateObject("MSXML2.ServerXMLHTTP")
    http.Open "GET", myURL, False
    http.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    http.send  
    MsgBox http.responseText
    
    end sub
    
    'http://stackoverflow.com/questions/2360153/use-excel-vba-to-fill-out-and-submit-google-docs-form/28079922#28079922
    
    Dim savedname
    
    Sub sendresult()
    
    
    Dim ScriptEngine
    Set ScriptEngine = CreateObject("MSScriptControl.ScriptControl")
    ScriptEngine.Language = "JScript"
    ScriptEngine.AddCode "function encode(str) {return encodeURIComponent(str);}"
    
    Dim name, points, times, t1, t2, t3, t4
    
    times = Sheet5.Range("C13").Value
    
    If times = "0" Or times = "" Then
    MsgBox "no data"
    Exit Sub
    End If
    
    If savedname = Empty Then savedname = InputBox("enter your name")
    
    name = ScriptEngine.Run("encode", savedname)
    points = Sheet5.Range("C12").Value
    t1 = Sheet5.Range("C7").Value
    t2 = Sheet5.Range("C8").Value
    t3 = Sheet5.Range("C9").Value
    t4 = Sheet5.Range("C10").Value
    
    
    Dim myURL
    myURL = "https://docs.google.com/forms/d/123-ycyAMD4/formResponse?" & _
     "entry.1237336855=" & name & _
    "&entry.2099352330=" & points & _
    "&entry.962062701=" & times & _
    "&entry.1420067848=" & t1 & _
    "&entry.6696464=" & t2 & _
    "&entry.1896090524=" & t3 & _
    "&entry.1172632640=" & t4
    
    
    Dim http
    Set http = CreateObject("MSXML2.ServerXMLHTTP")
    http.Open "GET", myURL, False
    http.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    http.send
    Dim resp
    
    If UBound(Split(http.responseText, "<div class=""ss-resp-message"">")) > 0 Then
     resp = Split(Split(http.responseText, "<div class=""ss-resp-message"">")(1), "</div>")(0)
    Else
     resp = "sent(with unexpected server response)"
    End If
    If resp = "Your response has been recorded." Then resp = "input received"
    MsgBox resp
    
    
    End Sub