Using Aspose.Word through Microsoft SQL Server works Great BUT!


#1

Hi there,

I’m a SQL Goeroe who is implementing automatic mailmerging via SQL Server with Aspose.

Well, talking to the Aspose component is not a problem except for calling the constructor with a filename which is not possible.

I can create a new instance with an new empty document and perform all routines I want.

What I cannot do is open an existing document! I’ve tried using the .Template propterie of the DocumentBuildInProperties property but this just add a template instead of an existing document.

Does anyone know which method (other than the constructor) to call to provide an existing document. Maybe through Node?

Any help will be appreciated.

Kind regards,
Danny


#2

Hi Danny,

Thank you for your interest in Aspose.Word.

Are you using the component inside a COM application? If so, use the Word.Open method to load the document from a file or stream. Otherwise, please describe why you failed to use the constructor to open the document.


#3

Hi DmitryV,

Thnkx for your reply. I will past a piece of code I use in a SQL Server Stored Procedure:

DECLARE @WordDocument int
DECLARE @WordApplication int
DECLARE @documentProperties int
DECLARE @Mailmerge int
DECLARE @visible int
DECLARE @hr int
DECLARE @text varchar(4096)

-- Set WordDocument = CreateObject("Word.Document")
EXEC @hr = sp_OACreate 'Aspose.Word.Document', @WordDocument OUT
PRINT @hr

IF @hr = 0
EXEC @hr = sp_OAGetProperty @WordDocument, 'BuiltInDocumentProperties', @documentProperties OUT
PRINT @hr

IF @hr = 0
EXEC @hr = sp_OASetProperty @documentProperties, 'Template', 'c:\mailmerge.doc'
PRINT @hr


The sp_OA* procedures are builtin SQL Server procedures that allows a SQL Server programmer to "reach" OLE capable components / program's like Aspose.

I did not try the Open function because it is not listed in your API website. I will try this now


#4

Well, I’ve tried to use the Open() method which works. Except that I can either make an object of Aspose.Word.Word OR Aspose.Word.Document. If I create an object of Aspose.Word.Word I can open an existing document. I need members and methods of Aspose.Word.Document to work on my document. Those are not available from my Aspose.Word.Word object. Are you with me?

I have two different objects who cannot interact with each other.

What I need is an Open method in the Aspose.Word.Document object. Is that possible?

Regards,
Danny


#5

The point is that Word.Open returns a Document object that you should use to work, that’s the interaction. Is it possible to implement in your code?


#6

Hi Dmitry,

I’m sorry, that won’t work. SQL Server has ‘interfaces’ to COM components. It therefore has no reserved memory space to store or transfer objects in.

Danny


#7

I’m not familiar with SQL Server support for COM inside stored procedures, but even from your code sample above it sounds like it should all work fine.

To create a new document from a COM client you need to do this:

Dim doc
Set doc = CreateObject(“Aspose.Word.Document”)

To open an existing document from a COM client you do this:

Dim word
Set word = CreateObject(“Aspose.Word.Word”)
Dim doc
Set doc = word.Open(“myfile.doc”)


In SQL you already retrieve and store pointers to COM objects, for example Document.BuiltInDocumentProperties returns a COM interface to you. You retrieve it using sp_OAGetProperty and store int an OUT variable. I think there must be some other sp_XXX that allows to call a method and the result will be in the last OUT parameter.

This is how it probably looks in SQL Server:

EXEC @hr = sp_OACallMethod @word, ‘Open’, “myFile.doc”, @document OUT


#8

This works! You're right, I have a sp_OAMethod procedure. I overlooked the OUTPUT parameter. Your reply did the job! Thanks.

My code now looks like this:

--Sql Server OLE Automation example by Giuseppe Dimauro 04/2000
DECLARE @Word int
DECLARE @WordOpen int
DECLARE @WordDocument int
DECLARE @Mailmerge int
DECLARE @visible int
DECLARE @hr int
DECLARE @text varchar(4096)

-- Set WordDocument = CreateObject("Word.Document")
EXEC @hr = sp_OACreate 'Aspose.Word.Word', @Word OUT
PRINT @hr

IF @hr = 0
EXEC @hr = sp_OAMethod @Word, 'Open', @WordDocument OUT, 'c:\mailmerge.doc'
PRINT @hr

IF @hr = 0
EXEC @hr = sp_OAGetProperty @WordDocument, 'MailMerge', @Mailmerge OUT
PRINT @hr

--EXEC @hr = sp_OAMethod @MailMerge, 'Execute', NULL, 'test', 'Hallo allemaal'
--PRINT @hr

EXEC @hr = sp_OAMethod @WordDocument, 'Save', NULL, 'C:\mailmerge_result.doc'
PRINT @hr