Table of Contents

Codeunit "Word Template"

ID 9987
Namespace: System.Integration.Word

Exposes functionality to create and consume Word templates.

Properties

Name Value
Access Public

Methods

DownloadTemplate

Downloads the set template.

procedure DownloadTemplate()

DownloadDocument

Downloads the resulting document.

procedure DownloadDocument()

GetTemplate

Gets an InStream for the template.

procedure GetTemplate(var InStream: InStream)

Parameters

Name Type Description
InStream InStream

Out parameter, the InStream to set.

GetDocument

Gets an InStream for the resulting document.

procedure GetDocument(var DocumentInStream: InStream)

Parameters

Name Type Description
DocumentInStream InStream

Out parameter, the InStream to set.

GetMergeFields

Gets the merge fields used in the template.

procedure GetMergeFields(var Value: List of [Text])

Parameters

Name Type Description
Value List of [Text]

Out parameter, the list of merge fields returned.

GetDocumentSize

Gets size for the resulting document.

procedure GetDocumentSize(): Integer

Returns

Type Description
Integer

The size for the resulting document in bytes.

Create

Creates a template with the fields of a table. The table is selected by the user via a popup window.

procedure Create()

Create

Creates a template with the fields of the given table.

procedure Create(TableId: Integer)

Parameters

Name Type Description
TableId Integer

Specifies the ID of the table whose fields will be used to populate the template.

Create

Creates a template with the fields from a selected table and a list of related table IDs.

procedure Create(TableId: Integer, RelatedTableIds: List of [Integer], RelatedTableCodes: List of [Code[5]])

Parameters

Name Type Description
TableId Integer

Specifies the ID of the table from which fields will be used to insert data in the template.

RelatedTableIds List of [Integer]

Specifies the IDs of tables that are related to the selected table. Fields from these tables will also be used to insert data in the template.

RelatedTableCodes List of [Code[5]]

Specifies the IDs for each related table. The IDs must be the same length as the RelatedTableIds, and be between 1 and 5 characters.

Create

Creates a template with given merge fields.

procedure Create(MergeFields: List of [Text])

Parameters

Name Type Description
MergeFields List of [Text]

Names of mail merge fields to be available in the template.

Load

Loads the template to be used for merging.

procedure Load(WordTemplateCode: Code[30])

Parameters

Name Type Description
WordTemplateCode Code[30]

The code of the Word template to use.

Load

Loads the template to be used for merging.

procedure Load(WordTemplateStream: InStream)

Parameters

Name Type Description
WordTemplateStream InStream

InStream of the Word template to use.

Load

Loads the template to be used for merging from the stream and additional related fields from the WordTemplateCode which is used as the base.

procedure Load(WordTemplateStream: InStream, WordTemplateCode: Code[30])

Parameters

Name Type Description
WordTemplateStream InStream

InStream of the Word template to use.

WordTemplateCode Code[30]

The Word template which contains the related tables and fields.

Merge

Performs mail merge on set template and given data. Output document is of type .docx.

procedure Merge(Data: Dictionary of [Text, Text])

Parameters

Name Type Description
Data Dictionary of [Text, Text]

Input data to be merged into the document. The key is the merge field name and value is the replacement value.

Merge

Performs mail merge on set template and given data. Output document type is of specified save format.

procedure Merge(Data: Dictionary of [Text, Text], SaveFormat: Enum "Word Templates Save Format")

Parameters

Name Type Description
Data Dictionary of [Text, Text]

Input data to be merged into the document. The key is the merge field name and value is the replacement value.

SaveFormat Enum System.Integration.Word."Word Templates Save Format"

Format of the document to generate.

Merge

Performs mail merge on set template and given data. Output document type is of specified save format.

procedure Merge(Data: Dictionary of [Text, Text], SaveFormat: Enum "Word Templates Save Format", EditDoc: Boolean)

Parameters

Name Type Description
Data Dictionary of [Text, Text]

Input data to be merged into the document. The key is the merge field name and value is the replacement value.

SaveFormat Enum System.Integration.Word."Word Templates Save Format"

Format of the document to generate.

EditDoc Boolean

Edit document in Word after merging. Only available if OneDrive has been enabled. If true, the default value for conflict behaviors is to replace the existing file in OneDrive.

Merge

Performs mail merge on set template and given data. Output document type is of specified save format.

procedure Merge(Data: Dictionary of [Text, Text], SaveFormat: Enum "Word Templates Save Format", EditDoc: Boolean, DocSharingConflictBehavior: Enum "Doc. Sharing Conflict Behavior")

Parameters

Name Type Description
Data Dictionary of [Text, Text]

Input data to be merged into the document. The key is the merge field name and value is the replacement value.

SaveFormat Enum System.Integration.Word."Word Templates Save Format"

Format of the document to generate.

EditDoc Boolean

Edit document in Word after merging. Only available if OneDrive has been enabled.

DocSharingConflictBehavior Enum System.Integration."Doc. Sharing Conflict Behavior"

The behavior when the file being uploaded has a conflict. Only used if EditDoc is true.

Merge

Performs mail merge on set template and data taken from the Record associated with the Document. Output document is of type .docx.

procedure Merge(SplitDocument: Boolean)

Parameters

Name Type Description
SplitDocument Boolean

Specifies whether a separate document per record should be created.

Merge

Performs mail merge on set template and data taken from the Record associated with the Document. Output document type is of specified save format.

procedure Merge(SplitDocument: Boolean, SaveFormat: Enum "Word Templates Save Format")

Parameters

Name Type Description
SplitDocument Boolean

Specifies whether a separate document per record should be created.

SaveFormat Enum System.Integration.Word."Word Templates Save Format"

Format of the document to generate.

Merge

Performs mail merge on set template and data taken from the given Record. Output document is of type .docx.

procedure Merge(RecordVariant: Variant, SplitDocument: Boolean)

Parameters

Name Type Description
RecordVariant Variant

The Record to take data from, any filters on the Record will be respected.

SplitDocument Boolean

Specifies whether a separate document per record should be created.

Merge

Performs mail merge on set template and data taken from the given Record. Output document type is of specified save format.

procedure Merge(RecordVariant: Variant, SplitDocument: Boolean, SaveFormat: Enum "Word Templates Save Format")

Parameters

Name Type Description
RecordVariant Variant

The Record to take data from, any filters on the Record will be respected.

SplitDocument Boolean

Specifies whether a separate document per record should be created.

SaveFormat Enum System.Integration.Word."Word Templates Save Format"

Format of the document to generate.

Merge

Performs mail merge on set template and data taken from the given Record. Output document type is of specified save format.

procedure Merge(RecordVariant: Variant, SplitDocument: Boolean, SaveFormat: Enum "Word Templates Save Format", EditDoc: Boolean)

Parameters

Name Type Description
RecordVariant Variant

The Record to take data from, any filters on the Record will be respected.

SplitDocument Boolean

Specifies whether a separate document per record should be created.

SaveFormat Enum System.Integration.Word."Word Templates Save Format"

Format of the document to generate.

EditDoc Boolean

Edit document in Word after merging. Only available if OneDrive has been enabled.

Merge

Performs mail merge on set template and data taken from the given Record. Output document type is of specified save format.

procedure Merge(RecordVariant: Variant, SplitDocument: Boolean, SaveFormat: Enum "Word Templates Save Format", EditDoc: Boolean, DocSharingConflictBehavior: Enum "Doc. Sharing Conflict Behavior")

Parameters

Name Type Description
RecordVariant Variant

The Record to take data from, any filters on the Record will be respected.

SplitDocument Boolean

Specifies whether a separate document per record should be created.

SaveFormat Enum System.Integration.Word."Word Templates Save Format"

Format of the document to generate.

EditDoc Boolean

Edit document in Word after merging. Only available if OneDrive has been enabled.

DocSharingConflictBehavior Enum System.Integration."Doc. Sharing Conflict Behavior"

The behavior when the file being uploaded has a conflict. Only used if EditDoc is true.

AddTable

Add a table to the list of available tables for Word templates.

procedure AddTable(TableID: Integer)

Parameters

Name Type Description
TableID Integer

The ID of the table to add.

GetTableId

Get the table ID for this Template.

procedure GetTableId(): Integer

Returns

Type Description
Integer

Remarks

The function Load needs to be called before this function.

AddRelatedTable

Add related table.

procedure AddRelatedTable(WordTemplateCode: Code[30], RelatedCode: Code[5], TableId: Integer, RelatedTableId: Integer, FieldNo: Integer): Boolean

Parameters

Name Type Description
WordTemplateCode Code[30]

The code of an existing parent Word template.

RelatedCode Code[5]

The code of the related table to add.

TableId Integer

The code of an existing parent Word template.

RelatedTableId Integer

The code of an existing parent Word template.

FieldNo Integer

The field no. of the parent table that references the related table.

Returns

Type Description
Boolean

True if the related table was added, false otherwise.

Remarks

The function shows a message if the related code or table ID is already used for the parent table

SetDefaultFields

Set the default fields for the table.

procedure SetDefaultFields(WordTemplateCode: Code[30], TableId: Integer)

Parameters

Name Type Description
WordTemplateCode Code[30]

The code of an existing parent Word template.

TableId Integer

The code of an existing parent Word template.

AddUnrelatedTable

Adds an unrelated table entry for the specified WordTemplateCode.

procedure AddUnrelatedTable(WordTemplateCode: Code[30], PrefixCode: Code[5], UnrelatedTableId: Integer, RecordSystemId: Guid): Boolean

Parameters

Name Type Description
WordTemplateCode Code[30]

The code of an existing parent Word template.

PrefixCode Code[5]

The code of the unrelated table to add.

UnrelatedTableId Integer

The ID of the unrelated table to add.

RecordSystemId Guid

The system id of the record to add.

Returns

Type Description
Boolean

True if the unrelated table was added; otherwise, false.

SetExcludedFields

Set the fields to be excluded from the word template.

procedure SetExcludedFields(WordTemplateCode: Code[30], TableId: Integer, ExcludeFieldNames: List of [Text[30]])

Parameters

Name Type Description
WordTemplateCode Code[30]

The code of an existing parent Word template.

TableId Integer

The ID of the table.

ExcludeFieldNames List of [Text[30]]

The fields that should be excluded.

GetExcludedFields

Get the fields to be excluded from the word template.

procedure GetExcludedFields(WordTemplateCode: Code[30], TableId: Integer): List of [Text[30]]

Parameters

Name Type Description
WordTemplateCode Code[30]

The code of an existing parent Word template.

TableId Integer

The ID of the table.

Returns

Type Description
List of [Text[30]]

The fields that are being excluded.

GetCustomTableFields

Get a list of all current custom table fields for a given table.

procedure GetCustomTableFields(TableId: Integer): List of [Text[30]]

Parameters

Name Type Description
TableId Integer

The ID of the table.

Returns

Type Description
List of [Text[30]]

The names of the custom fields.

GetSupportedTableFields

Get a list of all supported table fields. This list does not include custom fields.

procedure GetSupportedTableFields(TableId: Integer): List of [Text[30]]

Parameters

Name Type Description
TableId Integer

The ID of the table.

Returns

Type Description
List of [Text[30]]

The names of all fields that are supported on the table.

IsFieldSupported

procedure IsFieldSupported(Field: __MissingTypeSymbol__): Boolean

Parameters

Name Type Description
Field __MissingTypeSymbol__

Returns

Type Description
Boolean

RemoveTable

Remove a related or unrelated table. An error is thrown if the table has any children, these must be removed first.

procedure RemoveTable(WordTemplateCode: Code[30], TableId: Integer): Boolean

Parameters

Name Type Description
WordTemplateCode Code[30]

The code of the parent Word template.

TableId Integer

The ID of the table to remove. This table must not have tables depending on it, otherwise an error will be thrown.

Returns

Type Description
Boolean

True if the table was removed, false otherwise.

GetChildren

Get all tables in this template which has a relation to the given table.

procedure GetChildren(WordTemplateCode: Code[30], TableId: Integer): List of [Integer]

Parameters

Name Type Description
WordTemplateCode Code[30]

The code of the parent Word template.

TableId Integer

The ID of the table.

Returns

Type Description
List of [Integer]

The tables related to this table.

Events

OnGetCustomFieldNames

Event to get custom field names for the word template based on the table id. Make sure to also subscribe to GetCustomRecordValues in order to provide the values for these fields.

[IntegrationEvent(False,False)]
internal procedure OnGetCustomFieldNames(var WordTemplateCustomField: Codeunit "Word Template Custom Field")

Parameters

Name Type Description
WordTemplateCustomField Codeunit System.Integration.Word."Word Template Custom Field"

Examples

if WordTemplateCustomFields.GetTableID() = Database::Customer then WordTemplateCustomFields.AddField('Customer Title');

OnGetCustomRecordValues

Event to add values for the custom fields specified through GetCustomFieldNames. In order to add a value for a field, it MUST be registered through the event GetCustomFieldNames.

[IntegrationEvent(False,False)]
internal procedure OnGetCustomRecordValues(var WordTemplateFieldValue: Codeunit "Word Template Field Value")

Parameters

Name Type Description
WordTemplateFieldValue Codeunit System.Integration.Word."Word Template Field Value"

Examples

RecRef := WordTemplateCustomFldValue.GetRecord(); if RecRef.Number = Database::Customer then WordTemplateCustomFldValue.AddFieldValue('Customer Title', GetCustomerTitle(RecRef));

See also