Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    184
    Thanks
    140
    Thanked 0 Times in 0 Posts

    Need help converting code from VBA to Javascript

    Hello,

    I need your help converting the code below from VBA to Javascript.

    I am basically attempting to export a recordset to an excel file.

    VBA:
    Code:
    '-------------------------------------------------------
    Public Sub ExportTOExcel()
    '-------------------------------------------------------
    
       Set oExcel = CreateObject("Excel.Application")
       Set oBook = oExcel.Workbooks.Add
       Set oSheet = oBook.Worksheets(1)
        Dim j As Long
        Dim lngCol As Long
        Dim lngRow As Long
        Dim Field As Object
        
        rs.MoveFirst
        lngRow = 1
        With oSheet
            Do While Not rs.EOF
                lngCol = 0
                For Each Field In rs.Fields
                    lngCol = lngCol + 1
                    .Cells(lngRow, lngCol) = Field.Value
                Next
                lngRow = lngRow + 1
                rs.MoveNext
            Loop
        End With
        
        oBook.SaveAs "C:\Book5.xls"
        Workbooks.Open fileName:="C:\Book5.xls"
        
    End Sub
    Thanks for everyones help in advance,

    Cheers,

    J

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,519
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Not to ask a silly question, but...

    Since this code will never work in any browser except MSIE, why not code it in VBScript instead of JScript?

    Then your code conversion is nearly painless.

    Code:
    <script type="text/vbscript">
    Sub ExportTOExcel()
    
       Set oExcel = CreateObject("Excel.Application")
       Set oBook = oExcel.Workbooks.Add
       Set oSheet = oBook.Worksheets(1)
        Dim j  ' As Long  (VBScript is untyped, like JS)
        Dim lngCol '  As Long
        Dim lngRow ' As Long
        Dim Field  ' As Object
        
        rs.MoveFirst
        lngRow = 1
        With oSheet
            Do While Not rs.EOF
                lngCol = 0
                For Each Field In rs.Fields
                    lngCol = lngCol + 1
                    .Cells(lngRow, lngCol) = Field.Value
                Next
                lngRow = lngRow + 1
                rs.MoveNext
            Loop
        End With
        
        oBook.SaveAs "C:\Book5.xls"
        Workbooks.Open "C:\Book5.xls" ' ??? not sure about this one
        
    End Sub
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •