Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 OLE Automation

Author  Topic 

stephenbow
Starting Member

2 Posts

Posted - 2011-10-29 : 11:57:52
I can no longer create an Excel workbook in T-Sql and save it to disk. My script has worked for years, and not it fails when I try to save the file to disk. Must be permissions, but which?

This is the script. Please can someone tell me why it no longer works?

DECLARE
@objExcel Int,
@objWorkBook Int,
@objWorkSheet Int,
@HR Int --Hold value that says if EXEC command worked or not

-------------------------------------------------------------------------------------------------------
--Create Excel application
EXEC @HR = sp_OACreate 'Excel.Application', @objExcel OUT
print @HR

---------------------------------------------------------------------
--create a workbook
EXEC @HR = sp_OAGetProperty @objExcel, 'WorkBooks.Add', @objWorkBook OUT
print @HR

---------------------------------------------------------------------
--add a worksheet to the workbook
EXEC @HR = sp_OAGetProperty @objWorkBook, 'worksheets.Add', @objWorkSheet OUT
print @HR
---------------------------------------------------------------------

--Save spreadsheet to nominated folder
EXEC @HR = master.dbo.sp_oaMethod @objWorkbook ,'SaveAs',null,'C:\temp.xls'
print @HR

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-29 : 12:16:08
whats the error you're getting? Can you provide more details on the error?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

paultech
Yak Posting Veteran

79 Posts

Posted - 2011-10-30 : 05:54:00
Ok,Please look to me Carefully
quote:

Instead of creating the Excel spreadsheet with OLEDB One can use the

sp_makewebtask



Users must have SELECT permissions to run a specified query and CREATE PROCEDURE permissions in the database in which the query will run. The SQL Server account must have permissions to write the generated HTML document to the specified location. Only members of the sysadmin server role can impersonate other users.

*/



sp_makewebtask @outputfile = 'c:\CambridgePubsHTML2.xls',

@query = 'Select * from ##CambridgePubs',

@colheaders =1,

@FixedFont=0,@lastupdated=0,@resultstitle='Cambridge Pubs',

@dbname ='MyDatabaseName'



/* This is fine for distributing information from databases but no good

if you subsequently want to open it via ODBC.*/


For more details just go to
http://www.simple-talk.com/sql/t-sql-programming/sql-server-excel-workbench/#sixth
paul Tech
Go to Top of Page

stephenbow
Starting Member

2 Posts

Posted - 2011-10-30 : 12:10:36
The OLE Automation error number is -2146827284 which translates into this

Microsoft Excel
Unable to get the SaveAs property of the Workbook class
Go to Top of Page
   

- Advertisement -