Click here to Skip to main content
6,596,602 members and growing! (20,964 online)
Email Password   helpLost your password?
Database » SQL Reporting Services » General     Intermediate

How to Print a Document from SQL Stored Procedure

By aleksisa

Print a document from a stored procedure using a SQL OLE and VB DLL
VB, VBScript, SQL, Windows, .NET, Visual Studio, DBA, Dev
Posted:6 Feb 2007
Views:23,799
Bookmarked:32 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 4.03 Rating: 4.22 out of 5

1

2
2 votes, 22.2%
3
3 votes, 33.3%
4
4 votes, 44.4%
5

Introduction

There're many different ways of pulling data out of the database and then format/print it according to your needs. The code bellow allows you to print a document from a stored procedure using a SQL OLE and VB DLL. ( Original Article - At Siccolo Articles archive

1.

At First, let's build a simple VB DLL (ActiveX component) that would handle the actual printing using Microsoft Word:


In ActiveX component, add a method that would print a document using Microsoft Word:
Public Sub PrintDocumentFromWord(ByVal DocumentFileName As String)

    On Error GoTo Err_Error
    
    Dim MethodName As String
    MethodName = ".PrintWebDocumentFromWord()"
    
    Dim strMessage As String
    
    Dim blnResult As Boolean
    
    Dim objWord As New Word.Application
    'Dim objWord As Object

    'Set objWord = CreateObject("Word.Application")

    
   
    Const wdAlertsNone = 0
    objWord.DisplayAlerts = wdAlertsNone
    
    
    'objWord.Activate

    'objWord.PrintOut FileName:=DocumentFileName

    Dim objDocument As Word.Document
    
    Set objDocument = objWord.Documents.Open(DocumentFileName)
    
    
    objDocument.Activate
    
    objDocument.PrintOut
    
    
    objDocument.Close
    
    
    objWord.Quit
    
       
Exit_Procedure:
    Set objDocument = Nothing
    Set objWord = Nothing
    
    Exit Sub

Err_Error:
    
    'handle error here

    Call HandleError("PrintWebDocument", ApplicationName, MethodName, VBA.Error)
    
    If Not objWord Is Nothing Then
        objWord.Quit
    End If
    
    Resume Exit_Procedure
    
End Sub
 

Compile and build ActiveX component. ActiveX component will reside on the same machine where SQL Server instance is running.

2.

Next, create a stored procedure that calls ActiveX component and passes file name of the document to be printed:
    
CREATE procedure sp_Print_Letter_File
(
    @file_name varchar(333)
    ,@debug_mode char(1)=''
)
as
    set nocount on

    declare @return int

    declare @print_document int
    declare @hr int
    declare @src varchar(255), @desc varchar(255)

    exec @hr = master.dbo.sp_OACreate 'PrintDocument.clsPrintDocument', @print_document OUT
    if @hr <> 0    -- error creating instance!

    begin
           exec master.dbo.sp_OAGetErrorInfo  @print_document, @src out, @desc out 
        select 'Creating Instance', hr=convert(varbinary(4),@hr), source=@src, description=@desc
        set @return = -1
        goto error
    end

if @debug_mode<>''
    print '1. created'


    exec @hr = sp_OAMethod @print_document, 'PrintDocumentFromWord',null,@file_name, @debug_mode
    if @hr <> 0
    begin
           exec sp_OAGetErrorInfo @print_document, @src OUT, @desc OUT 
        select 'Call to Method', hr=convert(varbinary(4),@hr), Source=@src, Description=@desc

        exec @hr = sp_OADestroy @print_document
        if @hr <> 0
        begin
            exec sp_OAGetErrorInfo @print_document, @src out, @desc out 
            select 'Destroing Instance',hr=convert(varbinary(4),@hr), source=@src, description=@desc
            --return

        end
        set @return = -2
        goto error

    end

if @debug_mode<>''
    print '2. send to object'


error:

    exec @hr = sp_OADestroy @print_document
    if @hr <> 0
    begin
        exec sp_OAGetErrorInfo @print_document, @src out, @desc out 
        select 'Destroing Instance',hr=convert(varbinary(4),@hr), source=@src, description=@desc
        set @return = -3
    end

if @debug_mode<>''
    print '3. done!'
    
set nocount off

GO
   

And that's it. Don't forget to register your ActiveX component (I usually do it with Component Services MMC snap-in)


If you would like to read more on this story - please take a look at Siccolo - Free Mobile Management Tool For SQL Server and more articles at Siccolo Articles

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

aleksisa


Member

Location: United States United States

Other popular SQL Reporting Services articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralHow to Print a Document from page Pinmemberm2kannan23:37 16 Apr '09  
GeneralHow to Print Pinmemberadam amin2:05 29 Oct '08  
GeneralCan already call COM objects in SQL 2000 Pinmembertrevorde stickman14:31 14 Feb '07  
GeneralRe: Can already call COM objects in SQL 2000 Pinmemberaleksisa9:31 15 Feb '07  
GeneralLicencing issue Pinmembertrevorde stickman14:23 14 Feb '07  
GeneralRe: Licencing issue Pinmemberaleksisa9:29 15 Feb '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Feb 2007
Editor:
Copyright 2007 by aleksisa
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project