Xbasic

a5_word_merge_dotnet Function

Syntax

a5_word_merge_dotNet as p (folder as c, template as c, connectionString as c, sql as c, args as sql::arguments, flagPortableSQL = .t., outputFilename = "")

Arguments

folderCharacter

The location where the word template file is stored.

templateCharacter

The filename for the word template to use.

connectionStringCharacter

A named SQL connection. E.G. "AADemo-Northwind".

sqlCharacter

The SQL query to execute.

argsSQL::Arguments

A SQL::Arguments object containing any arguments used in the SQL query.

flagPortableSQLLogical

Default = .t.. Specifies whether or not the SQL query uses portable SQL (.t.) or native syntax (.f.).

outputFilenameCharacter

Default = "". The name of the word document generated for each record. If blank, a filename will be generated using the format "MergeResult_rownumber" where rownumber is replaced by the record's row number in the result set. The filename can include placeholders for fields in the SQL records. For example:

dim outputFilename = "invoice for {CustomerId}"

Returns

resultPointer

Returns a dot variable with the following properties:

errorLogical

A .t. or .f. value. If .t., an error occurred and additional information can be found in the errorText property. If .f., the operation was successful.

errorTextCharacter

This property only exists if error is .t.. Contains information about the error.

Description

Merges data into an MS Word Template document. Requires Visual Studio Tools for Office.

Discussion

The a5_word_merge_dotNet() function does a SQL query against a named SQL connection and merges the resulting records into a Microsoft Word Template. The placeholders are mapped to the columns in the SQL result based on the column name. If the column name matches the placeholder in the Word template, the data for the record is merged into the template.

The Visual Studio Tools for Office is required to use this function. Download and install the Microsoft Office 2010: Primary Interop Assemblies Redistributable on the machine that will run this function.

One Word file is created for each record in the SQL query. The optional outputFilename parameter can be used to define the filename used for the merged Word documents. The filename can include placeholders for fields in the SQL results. If no output filename is specified, Alpha Anywhere automatically generates the filename as "MergeResult_rownumber" where rownumber is replaced with the record's row number in the SQL query result.

dim folder as c = "c:\wordTemplates"
dim template as c = "Template.docx"
dim conn as C = "::Name::Northwind"
dim sql as c = "SELECT customerid, contactname, address, city, region, country, postalcode, contactname AS salutation FROM customers WHERE country = :whatcountry"
dim args as sql::arguments
args.add("whatcountry","uk")
dim outputfilename as c
outputFilename = "{customerid}_invoice"

dim result as p
result = a5_word_merge_dotNet(folder,template,conn,sql,args,.t.,outputFilename)

if (result.error) then
    ' an error occurred
    showvar(result.errorText)
end if

See Also