I need assistance with crafting a VBA script that can automatically fill out and submit a Google Form whenever a button is pressed in my Excel add-in. My aim is to monitor which features the users are engaging with in my program.
Here’s the HTML setup of the form field I’m focusing on:
<div class="form-container">
<div class="question-item required text-field">
<div class="form-entry">
<label for="field_1" class="question-title">
Employee ID
<span class="required-mark">*</span>
</label>
<input type="text"
id="field_1"
class="input-short"
value=""
name="field.1.response">
</div>
</div>
</div>
I tried this method, but I’m running into an error when attempting to set the field value:
Sub SubmitDataToForm()
Dim browser As Object
Set browser = CreateObject("InternetExplorer.Application")
On Error GoTo cleanup
With browser
.navigate "https://forms.google.com/myformurl"
Do While .busy: DoEvents: Loop
Do While .ReadyState <> 4: DoEvents: Loop
With .document.Forms(0)
.EmployeeID.Value = Application.UserName
.Department.Value = "IT-Support"
.submit
End With
Do While .busy: DoEvents: Loop
MsgBox "Form submitted successfully"
End With
cleanup:
browser.Quit
Set browser = Nothing
End Sub
The error occurs on the line where I try to assign the employee ID value. I suspect the problem is that VBA is unable to locate the form element by its name. Any advice on how to correctly reference form fields or explore alternative solutions would be appreciated.