How to use an ActiveX script task to import data into a new Excel file

Goal
  1. 1. Export data from the “pubs.dbo.authors” table to a new Excel file.
    2. The name of the Excel file should be derived from the current date and time.
    3. The first row in the Excel file shows the column names.

Real World Example

A manager of a shop can run this DTS package on a daily basis to transfer Customer order data from SQL Server into Excel files. The orders made on a particular day will be transferred to the Excel file with the date as the file name so that the shop manager can easily find out which Excel file includes what information.

Steps to Implement

  1. 1. In Enterprise Manager, open DTS designer.
    2. Right-click on the white space of the DTS designer and choose Package Properties.
    3. Go to the Global Variables tab and create a new global string variable called fileName, which will hold the name of the Excel file to be created.

How to use an ActiveX script task to import data into a new Excel file

4. Click on the How to use an ActiveX script task to import data into a new Excel file symbol in the DTS designer’s connection list to set up the first connection to the pubs database in your SQL Server.
5. Click on the How to use an ActiveX script task to import data into a new Excel file symbol in the DTS designer’s connection list to set up the second connection to Excel. Under the File name text box, specify a file name, such as C:\test.xls.

How to use an ActiveX script task to import data into a new Excel file

  1. 6. highlight the sql server and the excel connection; click the How to use an ActiveX script task to import data into a new Excel file symbol in the DTS designer’s Tasks list to create a Transfer Data Task from the pubs database to the Excel file.
    7. From the Tasks list, click on the How to use an ActiveX script task to import data into a new Excel file symbol to add the following ActiveX script task:
How to use an ActiveX script task to import data into a new Excel fileFunctionMain()
How to use an ActiveX script task to import data into a new Excel file
How to use an ActiveX script task to import data into a new Excel file
DimappExcel
How to use an ActiveX script task to import data into a new Excel file
DimnewBook
How to use an ActiveX script task to import data into a new Excel file
DimoSheet
How to use an ActiveX script task to import data into a new Excel file
How to use an ActiveX script task to import data into a new Excel file
dimoPackage
How to use an ActiveX script task to import data into a new Excel file
dimoConn
How to use an ActiveX script task to import data into a new Excel file
How to use an ActiveX script task to import data into a new Excel file
SetappExcel=CreateObject("Excel.Application")
How to use an ActiveX script task to import data into a new Excel file
SetnewBook=appExcel.Workbooks.Add
How to use an ActiveX script task to import data into a new Excel file
SetoSheet=newBook.Worksheets(1)
How to use an ActiveX script task to import data into a new Excel file
How to use an ActiveX script task to import data into a new Excel file
'SpecifythecolumnnameintheExcelworksheet
How to use an ActiveX script task to import data into a new Excel file

How to use an ActiveX script task to import data into a new Excel fileoSheet.Range(
"A1").Value="au_lname"
How to use an ActiveX script task to import data into a new Excel fileoSheet.Range(
"B1").Value="au_fname"
How to use an ActiveX script task to import data into a new Excel fileoSheet.Range(
"C1").Value="phone"
How to use an ActiveX script task to import data into a new Excel fileoSheet.Range(
"D1").Value="address"
How to use an ActiveX script task to import data into a new Excel fileoSheet.Range(
"E1").Value="city"
How to use an ActiveX script task to import data into a new Excel file
How to use an ActiveX script task to import data into a new Excel file
How to use an ActiveX script task to import data into a new Excel file
'SpecifythenameofthenewExcelfiletobecreated
How to use an ActiveX script task to import data into a new Excel file

How to use an ActiveX script task to import data into a new Excel fileDTSGlobalVariables(
"fileName").Value="C:"&Month(Now)&"-"&
How to use an ActiveX script task to import data into a new Excel file
Day(Now)&"-"&Year(Now)&"-"&Hour(Time)&"-"&Minute(Time)&"-"&
How to use an ActiveX script task to import data into a new Excel file
Second(Time)&".xls"
How to use an ActiveX script task to import data into a new Excel file
How to use an ActiveX script task to import data into a new Excel file
WithnewBook
How to use an ActiveX script task to import data into a new Excel file.SaveAsDTSGlobalVariables(
"fileName").Value
How to use an ActiveX script task to import data into a new Excel file.save
How to use an ActiveX script task to import data into a new Excel file
EndWith
How to use an ActiveX script task to import data into a new Excel file
How to use an ActiveX script task to import data into a new Excel fileappExcel.quit
How to use an ActiveX script task to import data into a new Excel file
How to use an ActiveX script task to import data into a new Excel file
'dynamicallyspecifythedestinationExcelfile
How to use an ActiveX script task to import data into a new Excel file

How to use an ActiveX script task to import data into a new Excel file
setoPackage=DTSGlobalVariables.parent
How to use an ActiveX script task to import data into a new Excel file
How to use an ActiveX script task to import data into a new Excel file‘connection
2istotheExcelfile
How to use an ActiveX script task to import data into a new Excel file
setoConn=oPackage.connections(2)
How to use an ActiveX script task to import data into a new Excel fileoConn.datasource
=DTSGlobalVariables("fileName").Value
How to use an ActiveX script task to import data into a new Excel file
How to use an ActiveX script task to import data into a new Excel file
setoPackage=nothing
How to use an ActiveX script task to import data into a new Excel file
setoConn=nothing
How to use an ActiveX script task to import data into a new Excel file
How to use an ActiveX script task to import data into a new Excel fileMain
=DTSTaskExecResult_Success
How to use an ActiveX script task to import data into a new Excel file
How to use an ActiveX script task to import data into a new Excel file
EndFunction

8. Highlight the ActiveX script task and the SQL server connection, then go to the Workflow in the menu. Choose “On success” so that this ActiveX script task will be executed before the Transfer Data Task. Here is how it should look:

How to use an ActiveX script task to import data into a new Excel file