Sample ASP Code

I’m an ASP and Aspose newbie, so please bear with me… I’m simply trying to use code I found to create a doc with ASP. Here is the code:

<%

Dim RS
Set RS = CreateObject("ADODB.Recordset")
RS.Open \_
"SELECT TOP 50 \* FROM Customers ORDER BY Country, CompanyName", \_
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb"

Dim Word 
Set Word = CreateObject("Aspose.Word.Word")

Dim Doc
Set Doc = Word.Open("CustomerLabels.doc")

Doc.MailMerge.ExecuteADO RS
Doc.Save "c:\inetpub\wwwroot\aspose.word\demos\documents Out VBScript.doc"

%>

I created a “Northwind.mdb” DSN and put this code into an asp file, but when I open it up in IE I get “page cannot be displayed”. Please help. Thanks!

Hmm… from what you said it looks like you really try to open it in IE. This is wrong because ASP stands for Active Server Pages and the script is normally intended to be executed on the server (IIS).

If you want to run an ASP page, you need to have IIS running on the machine, create a virtual directory with an application and put your .asp page there. Then you can request it using http:\localhost\myapp\mypage.asp, IIS will execute the script inside the page.

You don’t really have to use ASP and IIS for that (unless this is what you really want). You can just create a script file, save myscript.vbs and execute it using VBScript command line or use VB6 or whatever. Better yet, use C# or VB.NET.

romank, that’s what I did, create an ASP page and requested it from the web server. For example, http://localhost/testdoc.asp. I need for it to be accessible through our website so they can perform word mail merges with the database.

I don’t have VB.NET) or C#. I just want to make a simple ASP page that creates the doc. How do I do that?

You need to make sure there is an IIS application configured in the folder where the page is. IIS does not just run all pages, it has a notion of web application. Please see the management console for IIS. Maybe you better create a new IIS application or a virtual directory in the console.

Here is how my page looks like:

<%@ LANGUAGE="VBSCRIPT" %>
<%

Option Explicit
Dim RS
Set RS = CreateObject("ADODB.Recordset")
RS.Open \_
"SELECT \* FROM AsposeWordOrderDetails WHERE OrderId = 10444 ORDER BY ProductID", \_
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\X\Aspose\Aspose.Word\Demos\Database\Northwind.mdb"

Dim Word 
Set Word = CreateObject("Aspose.Word.Word")

Word.SetLicenseCOM "C:\X\Aspose\Aspose.Word\Aspose.Word.Test\License\Aspose.Word.Winform.Professional.lic"

Dim Doc
Set Doc = Word.Open("C:\X\Aspose\Aspose.Word\Demos\Documents\Invoice.doc")

Doc.MailMerge.ExecuteWithRegionsADO RS, "OrderDetails"

Doc.Save "C:\X\Aspose\Other\Invoice Out VB.doc"

%>

DONE

Well, it looks like I’m getting somewhere now. It generates “Invoice Out VB.doc” but it doesn’t merge any data into the doc. I also had to take out the license line because I’m using an evaluation version. Is it not merging because I’m using the evaluation?

It’s okay to use without a license - it will just insert evaluation watermark and some random text.

For mail merge to work successfully you need to make sure you do have records in the data source you pass to mail merge, there are some field names matching in the data source and in the document, also if you use mail merge regions, the name of the table specified in the document must match the name of the table you pass to mail merge.

I figured it out… I didn’t understand the concept about the merging. Here’s the final code I used:

<%@ LANGUAGE="VBSCRIPT" %>
<%

Option Explicit
Dim RS
Set RS = CreateObject("ADODB.Recordset")
RS.Open \_
"SELECT \* FROM AsposeWordOrderDetails WHERE OrderId = 10444 ORDER BY ProductID", \_
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\Aspose.Word\Demos\Database\Northwind.mdb"

Dim Word 
Set Word = CreateObject("Aspose.Word.Word")

'Word.SetLicenseCOM "C:\Inetpub\wwwroot\Aspose.Word\License\License.rtf"

Dim Doc
Set Doc = Word.Open("C:\Inetpub\wwwroot\Aspose.Word\Demos\Documents\Invoice.doc")

Doc.MailMerge.ExecuteWithRegionsADO RS, "OrderDetails"

RS.Close

RS.Open \_
"SELECT \* FROM AsposeWordOrders WHERE OrderId = 10444", \_
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\Aspose.Word\Demos\Database\Northwind.mdb"

Doc.MailMerge.ExecuteWithRegionsADO RS, "Orders"

RS.Close

RS.Open \_
"SELECT \* FROM AsposeWordOrderTotals WHERE OrderId = 10444", \_
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\Aspose.Word\Demos\Database\Northwind.mdb"

Doc.MailMerge.ExecuteWithRegionsADO RS, "OrderTotals"

Doc.Save "C:\Inetpub\wwwroot\Aspose.Word\Other\Invoice Out VB.doc"

%>

DONE

Thanks romank!