/* Create stored procedure */
CREATE PROCEDURE [dbo].[CS_SP_DMO_RefreshExcel]
AS
BEGIN
declare @hr int,
@objExcel int,
@objWorkBooks int,
@objWorkSheet int,
@WorksheetIndex int,
@filename varchar(512),
@strErrorMessage varchar(255),
@FindFile int, /* 0=False, -1=True */
@T int
set @filename = 'C:\Temp\Refresh.xls'
set @WorksheetIndex = 1
exec @hr = sp_OACreate 'Excel.Application', @objExcel out
if (@hr = 0)
select @strErrorMessage = 'Returning WorkBooks object '
IF (@hr = 0)
EXEC @hr = sp_OAMethod @objExcel,
'WorkBooks.Open',
@objWorkBooks OUT,
@filename
IF (@hr = 0)
BEGIN
Set @FindFile = -1
EXEC @hr = sp_OAMethod @objWorkBooks,
'Worksheets.Item',
@objWorkSheet OUT,
@WorksheetIndex
IF (@hr = 0)
PRINT ('Selection Worksheet successful')
ELSE
PRINT ('Selection Worksheet ended with errors')
IF (@hr = 0)
EXEC @hr = sp_OAMethod @objWorkSheet, 'Activate'
IF (@hr = 0)
PRINT ('Worksheet successful activation')
ELSE
PRINT ('Activation Worksheet ended with errors')
-- Refresh WorkSheet
IF (@hr = 0)
--EXEC @hr = sp_OAMethod @objWorkSheet, 'Refresh' -- ??
EXEC @hr = sp_OAMethod @objWorkSheet, 'Update' -- ??
IF (@hr = 0)
PRINT ('Refresh Worksheet successful')
ELSE
PRINT ('Refresh Worksheet ended with errors: ' + str(@hr))
EXEC @hr = sp_OAMethod @objExcel, 'Workbooks.Close'
EXEC sp_OAMethod @objExcel, 'Close'
END
-- Free memory
EXEC sp_OADestroy @objExcel
EXEC sp_OADestroy @objWorkSheet
EXEC sp_OADestroy @objWorkBooks
END
|