Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
MD MD is offline
external usenet poster
 
Posts: 1
Default How to run a excel macro from SQL

Is there a way to open a excel file from sql server (query window or DTS). I
want to run a macro on a excel file before I load the data from the file to
SQL server table using dts. If someone can post the code to that would be
very helpful

Thanks,
MG


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default How to run a excel macro from SQL

Hi MG,

There may be a better way, but you can use certain OLE-related Extended
Stored Procedures to automate other applications. For example, the T-SQL
code below may work for you. You may want to ask you question in a SQL
newsgroup, as there may be better ways of doing something like this.

Also, keep in mind that this merely opens the workbook - from there, your
workbook will have to take over. You can put some code in the Workbook_Open
event routine that calls another subroutine. In that subroutine, you can do
your processing, set ThisWorkbook.Saved=True, then invoke Application.Quit.
If you want to call a specific VBA routine from SQL after opening the
workbook, you would have to execute the "Run" method of the Application
object (in this case @object), passing in the appropriate parameters and
getting the return value, if any. But then you would have to set the Saved
property and invoke the Quit method as well, so it would make this code
substantially longer and slower.

declare @hr integer,
@object integer,
@wbs integer,
@src varchar(255),
@error varchar(255),
@test integer

EXEC @hr = sp_OACreate 'Excel.Application', @object OUTPUT
if (@hr < 0)
begin
EXEC sp_OAGetErrorInfo @object, @src OUTPUT, @error OUTPUT
PRINT 'Error: ' + @src + ', ' + @error
end
else -- successful creation of Excel
begin
EXEC @hr = sp_OAGetProperty @object, 'Workbooks', @wbs OUTPUT
if (@hr < 0)
begin
EXEC sp_OAGetErrorInfo @object, @src OUTPUT, @error OUTPUT
PRINT 'Error: ' + @src + ', ' + @error
end
else -- successful retrieval of wb collection
begin
EXEC @hr = sp_OAMethod @wbs, 'Open', @test OUTPUT, 'C:\test.xls'
if (@hr < 0)
begin
EXEC sp_OAGetErrorInfo @wbs, @src OUTPUT, @error OUTPUT
PRINT 'Error: ' + @src + ', ' + @error
end
else -- successful opening of wb
begin
EXEC sp_OADestroy @wbs
EXEC sp_OADestroy @object
PRINT 'Success!'
end
end
end

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


MD wrote:
Is there a way to open a excel file from sql server (query window or
DTS). I want to run a macro on a excel file before I load the data
from the file to SQL server table using dts. If someone can post the
code to that would be very helpful

Thanks,
MG


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro Help Needed - Excel 2007 - Print Macro with Auto Sort Gavin Excel Worksheet Functions 0 May 17th 07 01:20 PM
My excel macro recorder no longer shows up when recording macro jack Excel Discussion (Misc queries) 1 February 5th 07 09:31 PM
My excel macro recorder no longer shows up when recording macro jack Excel Discussion (Misc queries) 3 February 5th 07 08:22 PM
Macro 'Automation error' with ChemOffice Excel macro Stew Excel Programming 0 October 27th 03 08:26 PM


All times are GMT +1. The time now is 09:56 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"