Important alert: (current site time 7/15/2013 7:41:19 PM EDT)
 

VB icon

create a word document from a T-SQL SP

Email
Submitted on: 8/11/2000 11:24:01 AM
By: Umachandar  
Level: Intermediate
User Rating: By 6 Users
Compatibility: SQL Server 7.0, SQL Server 6.5 and earlier
Views: 23295
 
     How to create a word document from a T-SQL SP
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: create a word document from a T-SQL SP
-- Description:How to create a word document from a T-SQL SP
-- By: Umachandar
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=222&lngWId=5--for details.--**************************************

/********************************************************************************/
/*	Created By :	Umachandar Jayachandran					*/
/*	Created On :	23 July 1999						*/
/* 	Description:	Demonstrates OLE automation by using WORD.		*/
/********************************************************************************/
/*	Resources :	http://www.umachandar.com/resources.htm 	*/
/********************************************************************************/
Declare @WordObject int, @RetCode int, @Document int , @Filename varchar(255)
Exec @RetCode = sp_OACreate 'Word.Application', @WordObject OUTPUT, 4
If @RetCode <> 0 or @@Error <> 0 Goto OLE_Error_Handler
SELECT @WordObject
Exec @RetCode = sp_OAMethod @WordObject, 'Documents.Add', @Document OUTPUT
If @RetCode <> 0 or @@Error <> 0 Goto OLE_Error_Handler
Exec @RetCode = sp_OAMethod @WordObject,
		'Selection.TypeText("Created from within SQL Server SP using OLE Automation.")'
If @RetCode <> 0 or @@Error <> 0 Goto OLE_Error_Handler
-- There are several different methods to pass parameters to OLE
-- automation methods. These are discussed in BOL of SQL65/70.
/* -- This will also work fine.
DECLARE @Method varchar(255)
SELECT @Method = 'ActiveDocument.SaveAs("C:\Temp\Doc Created From SQLServer.doc")' 
Exec @RetCode = sp_OAMethod @WordObject, @Method		
*/
SELECT @Filename = 'C:\Temp\Doc Created From SQLServer.doc'
Exec @RetCode = sp_OAMethod @WordObject,
		'ActiveDocument.SaveAs' , NULL , @Filename
If @RetCode <> 0 or @@Error <> 0 Goto OLE_Error_Handler
Exec @RetCode = sp_OAMethod @WordObject, 'Quit'
Exec sp_OADestroy @WordObject
Goto Done
OLE_Error_Handler:
Exec sp_displayoaerrorinfo @WordObject, @RetCode
Exec @RetCode = sp_OAMethod @WordObject, 'Quit'
Exec sp_OADestroy @WordObject
Goto Done
Done:
Exec sp_OAStop


Other 133 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
11/9/2001 11:07:25 AMJeroen de Lange

Very nice, but it doesn't exactly solve my problem. What I want to do is inserting results from queries into the word document. If you could point me in the right direction for this I would be much obliged.
Instead of:

EXEC @RetCode = sp_OAMethod @WordObject,
'Selection.TypeText("Created FROM within SQL Server SP using OLE Automation.")'

I want this:

SELECT @Remarks = 'These remarks should be the ResultSet from a query'
EXEC @RetCode = sp_OAMethod @WordObject,
'Selection.TypeText(@Remarks)'

Kindly yours,

Jeroen de Lange
waterloo@wanadoo.nl
(If this comment was disrespectful, please report it.)

 
12/10/2003 2:23:56 PM

This solution works in instances where MS Word is installed on the database server, but many organizations do not have Office installed on their servers. Another, more scalable, option exists in producing an XML document using the Microsoft Word schemas provided you are using Office 2000, Office XP, or Office 2003 on your clients.
(If this comment was disrespectful, please report it.)

 
4/3/2006 12:46:37 AMKISU

Excellent Work
Keep it up
(If this comment was disrespectful, please report it.)

 
11/11/2008 2:18:18 AMSathieshkumar

This site is very useful.
Thanks
(If this comment was disrespectful, please report it.)

 
11/11/2008 2:20:46 AMSathieshkumar

I cant find sp_displayoaerrorinfo sp in my sql server2000. how can i include it.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.