Create an excel file from DTS ActiveX Task - sql

I need create ten different excel files from DTS ActiveX Task. Just create, not manipulated it. I just know that I have to use
CreateObject(Excel.Application)
and
CreateObject(Scripting.FileSystemObject)
Thanks for a while.

If you use SQL 2005+, see following URL
http://oakdome.com/programming/SSIS_ActiveXScript_to_ScriptTask.php
UPD. Added vbscript for file creation. Just use it in your DTS Script task
fileName = "d:\test_file.xls"
Set excelObj = CreateObject("Excel.Application")
excelObj.Visible = True
Set objWorkbook = excelObj.Workbooks.Add()
objWorkbook.SaveAs(fileName)
excelObj.Quit

Related

Clear cells in multiple sheets from SSIS by VBA

How I can use below VBA coding in SSIS vba. I want to clear cells(data) from multiple sheets from SSIS by VBA coding
sub cod()
ThisWorkbook.Worksheets("Case management details").Range("A2:K10000").clear
ThisWorkbook.Worksheets("interface Timeliness").Range("A2:G20000").clear
ThisWorkbook.Worksheets("Life Events").Range("A2:N10000").clear
End sub
I had the same problem here. If you decided to solve this using SSIS component try with Script Task.
After you create excel steps will be:
Add Script Task on control flow.
Double click on component.
Press 'Edit Script...' button.
Right-click on the project in new visual studio windows (should be something like 'ST_' and some guid) and click on Manage NuGet Packages...
From 'Browse' tab install:
Microsoft.Office.Interop.Excel
Microsoft.CSharp
Into namespaces region add these 2 lines:
using Microsoft.Office.Interop.Excel;
using Microsoft.CSharp.RuntimeBinder;
Copy below code into ScriptMain class:
var appExcel = new Excel.Application();
string filename = #"D:\PathToExcel\ExcelFile.xlsx";
var newBook = appExcel.Workbooks.Open(filename);
var oSheet1 = newBook.Worksheets["Case management details"];
oSheet1.Range("A2", "K10000").Delete();
newBook.Save();
appExcel.Workbooks.Close();
appExcel.Quit();
Close without saving the second visual studio window (where you write script code).
Run the package
Note:
Instead of hardcoded file location, you can replace it with a package variable. In this case 2nd line will be:
string filename = Dts.Variables["User::ExcelFilePath"].Value.ToString();
I tested this package before write answer and everything work perfect!
With this logic, create multiple sheet instance and delete what you want.
If something isn't clear or non-logical write me in a comment and I will make correction.

Close MS Project using VSTO

I have a VSTO on MS Project. I use VB.NET. What I need is when I press the button I created on the ribbon, it will perform some codes which will update the info of some task, however, I would need to close the MS Project automatically. I tried application.FileCloseEx(), but it only closes the file, the MS Project is still loaded. I need similar to clicking the x button of the window.
Thanks,
Gilbert
If your MS Project application object is represented by "appMSProject" then it's as simple as:
appMSProject.Quit
OR say in a macro running under Project:
Application.Quit
Here's how I do it in VBA from Excel or Access. As far as I can tell the objects & methods are the same in VB.NET. Bottom line is that I create an instance of the MS Project object which starts the app & opens a file, execute some work, close the file, then destroy the MS Project object by setting it to Nothing. That has the effect of closing the app. You can also use "appMSProject.Quit" followed by setting it to Nothing. Frankly the 2nd option looks more orderly & easier to understand in code. Anyway, here's a sample of the way I do it:
Dim appMSProject As MSProject.Application
Dim prjPrj As MSProject.Project
Dim strPrjFile As String
strPrjFile = "C:\where_is_my_file\file_name.mpp"
Set appMSProject = New MSProject.Application
appMSProject.FileOpenEx Name:=strPrjFile
Set prjPrj = appMSProject.ActiveProject
'''Do something in here with the prjPrj
'Close the file, in my case w/o saving
appMSProject.FileCloseEx pjDoNotSave
'Destroy the objects
Set prjPrj = Nothing
Set appMSProject = Nothing
FYI - In this example I'm doing background work so I don't show the app. I also use "early binding".
Here's an MSDN example that does show the app with more info on early -vs- late binding - https://msdn.microsoft.com/en-us/library/office/ff865152.aspx

Use DBEngine with Run-time Access

i try to connect my xls with access database. Below code work greate when i have installed full access program on my machine. Problem is when i try tu use it on machine what have only installed Run-time version of access.
I have use this references:
Visual Basic For Applications
Microsoft Excel 14.0 Object Library
OLE Automation
Microsoft Office 14.0 Object Library
Microsoft Forms 2.0 Object Library
When i try to run below code i get error: ActiveX component can't create object or return reference to this object (Error 429)
Sub mcGetPromoFromDB()
Application.ScreenUpdating = False
Dim daoDB As DAO.Database
Dim daoQueryDef As DAO.QueryDef
Dim daoRcd As DAO.Recordset
'Error on line below
Set daoDB = Application.DBEngine.OpenDatabase("K:\DR04\Groups\Functional\DC_Magazyn\Sekcja_Kontroli_Magazynu\Layout\dbDDPiZ.accdb")
Set daoRcd = daoDB.OpenRecordset("kwPromoIDX", dbOpenDynaset)
Dim tempTab() As Variant
For Each Article In collecArticle
daoRcd.FindNext "IDX = " & Article.Index
Article.PromoName = daoRcd.Fields(1).Value
Article.PromoEnd = "T" & Format(daoRcd.Fields(2).Value, "ww", vbMonday, vbUseSystem)
Next
Application.ScreenUpdating = True
End Sub
Is IDX an indexed field?
Is kwPromoIDX optimized for this specific purpose? I mean does it only contain the fields required for this update, or are you pulling extra useless fields? Perhaps something like "SELECT IDX, [Field1Name], [Field2Name] FROM kwPromoIDX" would be more efficient.
Since you are only reading the table records, and don't seem to need to actually edit them instead of dbOpenDynaset, use dbOpenSnapshot.
Just throwing out an idea here, you'd have to test to see if it made any difference, but perhaps you could try to reverse your logic. Loop through the recordset 1 by 1 and locate the IDX within your worksheet.
Another thing I've done in the past is use .CopyFromRecordset and copied the entire recordset into a temporary worksheet and done the juggling back and forth entirely within Excel, to eliminate the back and forth.
Lastly, another approach can be to quickly loop through the entire recordset and populate an array, collection, ... and then work with it instead of Access. This way the data is all virtual and you reduce the back and forth with Access.
You'll need to do some testing to see what works best in your situation.

Delete top row of excel file using SSIS

I have an excel file that has a header row which is a row that I want to delete. The header row in thsi file are the cells of A1 to W1 merged into one. This causes a problem when I try to read the file because I am expecting column names. Proper column names exist in the second row of the file, which is why I want to delete the first.
To accomplish this I thought I'd be able to use the 'Excel Source' item in SSIS since it supports a SQL option to write a query. What I want to do is something like this:
SELECT * from ExcelFile WHERE Row > 1
My file only has data in columns A thru W.
I don't know what syntax I can use in the query to do this. The query builder that is in the Excel Source item will allow me to do many things with columns but I don't see an option for doing anything with rows. Searching online and using the help didn't get me anywhere.
None of these solutions will work because the Excel driver will be confused by the merged first line. You won't be able to use any driver features such as skip first row to do this. You need to run some script to open the Excel file and delete the row manually.
There is some basic sample script at this site:
http://www.sqlservercentral.com/Forums/Topic1327014-1292-1.aspx
The code below is adapted from the code written by snsingh at that site.
You would obviously want to use connnection manager properties, not hard coded paths
Excel needs to be installed on the SSIS Server for it to work - this is the only way to use Excel automation.
Dim filename As String
Dim appExcel As Object
Dim newBook As Object
Dim oSheet1 As Object
appExcel = CreateObject("Excel.Application")
filename = "C:\test.xls"
appExcel.DisplayAlerts = False
newBook = appExcel.Workbooks.Open(filename)
oSheet1 = newBook.worksheets("Sheet1")
oSheet1.Range("A1").Entirerow.Delete()
newBook.SaveAs(filename, FileFormat:=56)
appExcel.Workbooks.Close()
appExcel.Quit()
You don't need to use a syntax.
Go to control flow..
Pull in a data flow task.
Add a excel file source...add a conection manager
With excel sheet.
Open your connection manager and then check the box which says.
Column names In first row. That's it and add ur destination.

Need to generate excel using vb script?

I am trying to run test cases using driver script, which i had developed in vb script.(below is some part of code that launches QTP), and lanuched qtp script generate XLS reports. now issue is that when i invoke test suite using vb driver script, it creates files which include result rows as well blank rows (upto 65635). this problem leads to result file(.XLS) sixed in MBs which should not include blank rows and must be in KBs.i also checked running test cases using QTP directly but i did not face any issues and size were in KB.
Dim qtApp, Test_Path
Dim BasePath
Dim qtTest
BasePath = "some path"
Set qtApp = CreateObject("QuickTest.Application")
qtApp.Launch
qtApp.Visible = True
Test_Path = BasePath & "\" & scriptname
qtApp.Open Test_Path, True
Set qtTest = qtApp.Test
qtTest.Run
qtTest.Close
qtApp.Quit
Set qtTest = Nothing
Set qtApp = Nothing
kindly some one please let me know why the same script running using external VBs gives such huge result file?
Thanks in advance,
Priyank Shah
Implement the temp sheet creation in run time and generate the excel sheets. Also make sure that you set nothing on each excel object you declared.
Set objExcel = Nothing