> ## Documentation Index
> Fetch the complete documentation index at: https://docs.abbyy.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Other sample scripts

> VBScript and JScript samples that turn the Pages moved event into a Document merge event and merge document registration parameters when documents merge.

These sample scripts show how to handle document merge events in ABBYY FlexiCapture. Each example is available in both VBScript and JScript.

## Turn the Pages moved event into a document merge event

This handler runs on the [Pages moved](/flexi-capture/appendix/scripts/moving-pages) event and transforms it into a document merge event. If all pages of a document are moved, that document is fully embedded into the target document. The `Events.OnDocumentsMerge` procedure handles the document merge event.

### VBScript

```vb theme={null}
Dim currentDocument, currentDocumentPagesMoved
set currentDocument = Pages( 0 ).Document
currentDocumentPagesMoved = 0

For i = 0 to Pages.Count - 1
    If Pages( i ).Document.Id <> currentDocument.Id Then
        ' The next document
        If currentDocument.Pages.Count = currentDocumentPagesMoved Then
            ' All pages are moved from the previous document. The event is spawned.
            Events.OnDocumentsMerge TargetDocument, currentDocument
        End If
        Set currentDocument = Pages( i ).Document
    Else
Next

If currentDocument.Pages.Count = currentDocumentPagesMoved Then
    ' All pages are moved from the last document. The event is spawned.
    Events.OnDocumentsMerge TargetDocument, currentDocument
End If
```

### JScript

```javascript theme={null}
var currentDocument = Pages( 0 ).Document;
var currentDocumentPagesMoved = 0;

for( i = 0; i < Pages.Count - 1; i++ ) {
    if( Pages( i ).Document.Id != currentDocument.Id ) {
        // The next document
        if( currentDocument.Pages.Count == currentDocumentPagesMoved ) {
            // All pages are moved from the previous document. The event is spawned.
            Events.OnDocumentsMerge( TargetDocument, currentDocument );
        }
        currentDocument = Pages( i ).Document;
    } else {
        currentDocumentPagesMoved = currentDocumentPagesMoved + 1;
    }
}

if( currentDocument.Pages.Count == currentDocumentPagesMoved ) {
    // All pages are moved from the last document. The event is spawned.
    Events.OnDocumentsMerge( TargetDocument, currentDocument )
}
```

## Merge registration parameters when documents merge

This handler merges document registration parameters on the document merge event. When documents merge into a target document, only the parameters that are missing from the target are added from the new documents.

Store this code in a shared module and call the `OnDocumentsMerge` procedure when documents merge. For more information, see [Turn the Pages moved event into a document merge event](#turn-the-pages-moved-event-into-a-document-merge-event).

### VBScript

```vb theme={null}
Sub MergeProperty(ByVal TargetDocument, ByVal propertyName, ByVal propertyValue)
    If TargetDocument.Properties.Get(propertyName) = "" Then
        ' There is no such a parameter in the document.
        TargetDocument.Properties.Set(propertyName, propertyValue)
    End If
End Sub

Sub OnDocumentsMerge(ByVal Target, ByVal Document)
    ' Merging registration parameters.
    For i = 0 To Document.Properties.Count - 1
        MergeProperty(Target, Document.Properties(i).Name, Document.Properties(i).Value)
    Next
End Sub
```

### JScript

```javascript theme={null}
function MergeProperty( TargetDocument, propertyName, propertyValue )
{
    if( TargetDocument.Properties.Get( propertyName ) == "" ) {
        // There is no such a parameter in the document.
        TargetDocument.Properties.Set( propertyName, propertyValue );
    }
}

function OnDocumentsMerge( Target, Document )
{
    // Merging registration parameters.
    for( i = 0; i < Document.Properties.Count - 1; i++ ) {
        MergeProperty( Target, Document.Properties(i).Name, Document.Properties(i).Value );
    }
}
```
