0

I am using these code. I have saved these file named (Book.html).

When I am opening Internet Explorer then I fill the form but my form data is not coming in excel sheet. Which entry I fill in HTML webpage.It should automatically come in excel sheet.

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <script language="vbscript" type="text/vbscript">
        Sub Sample (){
            Dim iRow
            Set objExcel = CreateObject("Excel. Application")
            Set objWorkbook = objExcel.Workbooks.Open("C:\Users\VAIBHAV BEDI\Desktop\Book1.xlsx")
            Set objWorksheet = objWorkbook.Worksheets("Book1")
            objExcel.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 objWorksheet = Nothing
            Set objWorkbook = Nothing
            Set objExcel = Nothing
        End Sub
        }
        </script>
        <form>
            First name:<br>
            <input name="fname" type="text" value=""><br>
            Last name :<br>
            <input name="lname" type="text" value=""><br>
            Address1 :<br>
            <input name="Add1" type="text" value=""><br>
            Address2 :<br>
            <input name="Add2" type="text" value=""><br>
            <br>
            <input id="Sample" onclick="Sample();" type="button" value=
            "Submit"><br>
            <form></form>
        </form>
    </body>
</html>
  • 2
    Welcome to SO. Instead of posting the code as a link, you should add the relevant code in your question itself. This will make it easier for people to understand the question and answer it. – UditS Jul 16 '16 at 14:35
  • What version of Internet Explorer? VBScript is no longer supported in IE11 edge mode https://msdn.microsoft.com/en-us/library/dn384057 – Slai Jul 16 '16 at 21:34
  • I am using Internet Explore 11 version – Vaibhav Bedi Jul 17 '16 at 05:20

1 Answers1

0

VBScript doesn't use curly brackets {}. You have space in CreateObject("Excel. Application"). <input id="Sample" onclick="Sample();" rename Sample() it's being used to identify the input element.

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <script language="vbscript" type="text/vbscript">
        Sub TestExcel()
            Dim iRow
            Set objExcel = CreateObject("Excel.Application")
            Set objWorkbook = objExcel.Workbooks.Open("C:\Users\VAIBHAV BEDI\Desktop\Book1.xlsx")
            Set objWorksheet = objWorkbook.Worksheets("Book1")
            objExcel.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 objWorksheet = Nothing
            Set objWorkbook = Nothing
            Set objExcel = Nothing
        End Sub

        </script>
        <form>
            First name:<br>
            <input name="fname" type="text" value=""><br>
            Last name :<br>
            <input name="lname" type="text" value=""><br>
            Address1 :<br>
            <input name="Add1" type="text" value=""><br>
            Address2 :<br>
            <input name="Add2" type="text" value=""><br>
            <br>
            <input id="Sample" onclick="TestExcel();" type="button" value=
            "Submit"><br>
        </form>
    </body>
</html>