Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-03-2012, 09:32 PM   PM User | #1
jason_kelly
Regular Coder

 
Join Date: Sep 2011
Posts: 140
Thanks: 88
Thanked 0 Times in 0 Posts
jason_kelly is an unknown quantity at this point
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
jason_kelly is offline   Reply With Quote
Old 05-03-2012, 10:00 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:07 AM.


Advertisement
Log in to turn off these ads.