This article explains how to store HTML form data to Microsoft Excel using a VBScript.
Step 1
- Start the Excel application.
- Create Header label text for each column as in the following diagram.
Figure 1: Data Column Header
- Save the Workbook with file extension “xlsx”.
- Open Notepad.
- Add the following code.
- <! DOCTYPE html>
- <Html>
- <Head>
- <script language="vbscript" type="text/vbscript">
- Sub Sample ()
- Dim iRow
- Set objExcel = CreateObject ("Excel. Application")
- Set objWorkbook = objExcel.Workbooks.Open ("C:\Users\Karthikeyan.K\Desktop\Book1.xlsx")
- objExcel.Application.Visible = True
- ObjWorkbook.Windows (1).Visible = True
- Set XlSheet =objWorkbook.Sheets (1)
- XlSheet.Activate
- iRow = 1
- With objExcel
- Do while .Cells (iRow, 1).value <> ""
- .Cells (iRow, 1).activate
- iRow = iRow + 1
- Loop
- .Cells (iRow, 1).value=Document.GetElementsByName ("fname") (0).Value
- .Cells (iRow, 2).value=Document.GetElementsByName ("lname") (0).Value
- .Cells (iRow, 3).value=Document.GetElementsByName ("Add1") (0).Value
- .Cells (iRow, 4).value=Document.GetElementsByName ("Add2") (0).Value
- MsgBox "Data Added Successfully”, vbinformation
- Document.GetElementsByName ("fname") (0).Value=""
- Document.GetElementsByName ("lname") (0).Value=""
- Document.GetElementsByName ("Add1") (0).Value=""
- Document.GetElementsByName ("Add2") (0).Value=""
- End With
- ObjWorkbook. Save
- ObjWorkbook. Close
- Set objWorkbook = Nothing
- Set objExcel = Nothing
- End Sub
- </script>
- <style type="text/css">
- fieldset {
- border: #00cc00 2px solid;
- padding: 10px;
- color: green;
- </style>
- <body
- <form>
- <fieldset>
- <legend>Csharpcorner</legend>
- <center>
- <img src="C:\Users\Karthikeyan.K\Desktop\Add-Male-User.png" alt="Mountain View"><br>
- First name:<br>
- <input type="text" name="fname" Value=""><br>
- Last name :< br>
- <input type="text" name="lname" Value=""><br>
- Address1 :< br>
- <input type="text" name="Add1" Value=""><br>
- Address2 :< br>
- <input type="text" name="Add2" Value=""><br>
- <br>
- <input type="button" onclick="Sample()" value="Submit" /><br>
- </center>
- </fieldset>
- <form>
- </body>
- </html>
- Save the file with the extension “.htm”.
Step 2
Follow the procedure to turn on or turn off VBScript in your Internet Explorer:
- Choose Internet Options from the Tools menu.
Figure 2
- Select the Security tab from the dialog box.
- Click the Custom Level button.
Figure 3
- Scroll down until you find the Scripting option.
Figure 4
- Select the Enable radio button under Active scripting.
- Finally click OK and get out.
Output
- Open the HTML document.
Figure 5: User Input Form
- Enter the data then press the Submit button.
Figure 6: User input data
Figure 7: Data added successfully
- Open the Workbook to check sheet1's data.
Figure 8: User data sheet
Conclusion
Thanks for reading! I hope you liked this article. Please provide your valuable suggestions.