Now Available!

Native .Net
Components

TList WinForms
for
.Net Framework

&

MetaDraw WinForms
for
.Net Framework



Bennet-Tec
Components
Make you
look sharp!



Home

Company

Products

Order Forms

How To

Downloads

Updates

Support

Registration

Dependencies

Links

Compatibility

Site Map


Basic How To


How To Implement Replaceable Fields

It is easy to implement replaceable fields in ALLText, however ALLText does not support the same RTF fields as created in MS Word.

View On-Line Sample
Return to main How To Page

Using Tags to Implement Mail Merge / Replaceable Fields

ALLText provides a mechanism for Tagging phrases within an RTF document. We call these NTags and HTags. Any number of phrases may be tagged in a document and each phrase can have it's own unique tag ( a long integer number) or you can use the same tag multiple times in a document.

It's a simple matter to implement a Mail Merge type application using ALLText and Tagged Phrases ( see "Creating Tagged Phrases" below). Simply include some placeholder text in the document and tag it with either NTags or HTags.

Now to conduct a MailMerge you can use the FindNTag or FindHTag method in a loop to search for and select each tagged phrase. Then read the associated Tag and replace the selected text using either the SelText or SelFText properties.

Creating Tagged Phrases

There are two ways of creating Tagged Phrases...

  • Use embedded codes

      \ATXht#### - starts an HTagged phrase, #### is any long integer value \ATXht0 - ends an HTagged phrase

      \ATXnt#### - starts an NTagged phrase, #### is any long integer value \ATXnt0 - ends an NTagged phrase these tags must be terminated by a space.

      You can use these embedded codes within a file to be loaded into ALLText, or you can use them when creating or modifying a document programmatically.

      To add embedded codes to an existing TXT or RTF document, open The document in Notepad or a similar text based editing tool ( Do not use MS Word or other RTF editor as this will treat embedded codes you type in as text, rather than as codes) Open the document and simply add the embedded codes as shown above.

      To add embedded codes you can include them as part of a string assigned to the FText or SelFText properties

      ALLText.SelFText = "Here is a sentence with \ATXht5 an HTagged \ATXht0 phrase"

      ALLText.SelFText = "Here is a sentence with \ATXnt5 an NTagged \ATXnt0 phrase"

  • Use the NTag and HTag properties while editing the document in ALLText Place a button or other interface element on your form. In the click event you can set the NTag or HTag property with any long integer value. This will apply the tag to any currently selected text.

    SAMPLE CODE:

    Private Sub Form_Load()

    ' Initialize ALLText with some text
    ' We could load from a file using FileLoad, but for this example we'll hard code in the initial document
    ' Text tagged with NTag values ( using \ATXnt ) will be replaced.

    ALLText.FText = "\plain \fs20 \li4005 \cf1 \ATXnt1 Date \ATXnt0 \cf0 " _
    & "\par \li0 \par \par \ATXnt3 Street Address\ATXnt0 " _
    & "\par \ATXnt4 City, State \ATXnt0" _
    & "\par\par Dear \ATXnt2 Name\ATXnt0 :" _
    & "\par \par This is a form letter. " _
    & "Here we will show how to handle Field Replacement inside ALLText"_
    & "\par \par For further information please contact us atSupport_ALLText@Bennet-Tec.Com"

    ' Turn on WriteProtection

    '(ATX_PROTECT_CHANGES = -1)

    ALLText.WriteProtect = ATX_PROTECT_CHANGES

    End Sub


    Sub cmdReplace_Click()

    ' This subroutine goes through the document and replaces any NTagged text

    Dim Ret_code&

    Dim Done As Integer

    Dim wp%

    With ALLText

    ' - Change to Hour Glass and freeze ALLText display

    Screen.MousePointer = 1

    ' remember write protect status

    wp% = ALLText.WriteProtect

    '(ATX_PROTECT_SCREEN = 2) - prevent screen refresh

    .WriteProtect = 2

    ' - Deselect any currently selected text and set cursor to end of control

    .Select = False

    .SelStart = .TextLength - 1

    ' - Loop until all tagged text is replaced

    Do Until Done = True

    '- Find tagged text

    ' check if first character is tagged

    If .NTag <> 0 Then

    ' select current phrase

    Ret_code& = .FindNTag(0, 0)

    Else

    ' select next tagged phrase

    Ret_code& = .FindNTag(0, -1)

    End If

    '- Replace tagged text

    Tag = .NTag

    Select Case Tag

    Case Is > 0

    ' replace the text as appropriate

    ' this function should be written to retrieve desired text

    x = GetText(Tag)

    .SelText = x

    ' reset the tag on the text

    .NTag = Tag

    ' turn off selection and move to start of previously selected text

    .Select = 0

    If .SelStart = 0 Then Done = True

    Case Is = 0

    Done = True

    End Select

    Loop

    ' - Return to standard mouse pointer and unfreeze ALLText display

    Screen.MousePointer = 0

    'screen refresh

    .WriteProtect = wp%

    End With

    End Sub


    Function GetText(ByVal WhatData As Long) As String

    ' Generally we might expect this information to come from a database

    ' but here we are simulating with fake data

    Dim Names(10) As String

    Names(0) = "Mr. Big Jaws"

    Names(1) = "Ms. Minnows"

    Names(2) = "Captain Nemo"

    Names(3) = "Little Mermaid"

    Dim Streets(10) As String

    Streets(0) = "50 Surf Road"

    Streets(1) = "35 Waterloo Way"

    Streets(2) = "1002 Little Pond Ave"

    Streets(3) = "2 Big Ocean Place"

    Dim Cities(10) As String

    Cities(0) = "Montauk, NY"

    Cities(1) = "SeaCliff, NY"

    Select Case WhatData

    Case 1
    GetText = Format$(Now(), "dd mmm yyyy")

    Case 2
    GetText = Names(Int(Rnd() * 4))

    Case 3
    GetText = Streets(Int(Rnd() * 4))

    Case 4
    GetText = Cities(Int(Rnd() * 2))

    End Select

    End Function


Copyright© 2003 Bennet-Tec Information Systems, Inc. All rights reserved.