kdjonesmtb2
Technical User
Hello,
How would I update this script to using an existing workbook and worksheet
set conn = createobject("adodb.connection")
conn.open("Provider=SQLOLEDB.1;Password=password;Persist Security Info=True;User ID=webuser;Initial Catalog=Northwind;Data Source=(local)")
set rs = conn.execute("select * from customers")
if not rs.eof then
set xlApp = CreateObject("Excel.Application")
Set xlWb = xlApp.Workbooks.Add
Set xlWs = xlWb.Worksheets("Sheet1")
' Display Excel and give user control of Excel's lifetime
xlApp.Visible = False
xlApp.UserControl = True
' Copy field names to the first row of the worksheet
fldCount = rs.Fields.Count
For iCol = 1 To fldCount
xlWs.Cells(1, iCol).Value = rs.Fields(iCol - 1).Name
Next
recArray = rs.GetRows(-1)
recCount = UBound(recArray, 2) + 1
xlWs.Cells(2, 1).Resize(recCount, fldCount).Value = TransposeDim(recArray)
'xlWs.Cells(2, 1).Resize(recCount, fldCount).Value = xlApp.WorksheetFunction.Transpose(recArray)
xlApp.Selection.CurrentRegion.Columns.AutoFit
xlApp.Selection.CurrentRegion.Rows.AutoFit
xlApp.visible = true
else
msgbox "empty"
end if
rs.close
set rs =nothing
conn.close
set conn = nothing
How would I update this script to using an existing workbook and worksheet
set conn = createobject("adodb.connection")
conn.open("Provider=SQLOLEDB.1;Password=password;Persist Security Info=True;User ID=webuser;Initial Catalog=Northwind;Data Source=(local)")
set rs = conn.execute("select * from customers")
if not rs.eof then
set xlApp = CreateObject("Excel.Application")
Set xlWb = xlApp.Workbooks.Add
Set xlWs = xlWb.Worksheets("Sheet1")
' Display Excel and give user control of Excel's lifetime
xlApp.Visible = False
xlApp.UserControl = True
' Copy field names to the first row of the worksheet
fldCount = rs.Fields.Count
For iCol = 1 To fldCount
xlWs.Cells(1, iCol).Value = rs.Fields(iCol - 1).Name
Next
recArray = rs.GetRows(-1)
recCount = UBound(recArray, 2) + 1
xlWs.Cells(2, 1).Resize(recCount, fldCount).Value = TransposeDim(recArray)
'xlWs.Cells(2, 1).Resize(recCount, fldCount).Value = xlApp.WorksheetFunction.Transpose(recArray)
xlApp.Selection.CurrentRegion.Columns.AutoFit
xlApp.Selection.CurrentRegion.Rows.AutoFit
xlApp.visible = true
else
msgbox "empty"
end if
rs.close
set rs =nothing
conn.close
set conn = nothing