Sample code for 30+ languages & platforms
SQL Server

Get a Mail-Merge Replacement String by Index

See more Email Object Examples

Demonstrates the Chilkat Email.GetReplaceString method, which returns the replacement string for the Nth previously-defined pattern/replacement pair (a mail-merge feature). The index is zero-based and corresponds to the same index used by GetReplacePattern. This example defines two pairs and prints each pattern with its replacement.

Background: Pairing GetReplacePattern (the token to find) with GetReplaceString (the text to substitute) at the same index lets you walk the full mail-merge table. Retrieving a replacement by its position is handy when enumerating all substitutions; to look one up by its pattern instead, use GetReplaceString2.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @sTmp1 nvarchar(4000)
    --  Demonstrates the GetReplaceString method, which returns the replacement string for the
    --  Nth previously-defined pattern/replacement pair (a mail-merge feature).  The index is
    --  zero-based and corresponds to the same index used by GetReplacePattern.

    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @email, 'Subject', 'Hello FIRST_NAME'
    EXEC sp_OASetProperty @email, 'Body', 'Dear FIRST_NAME, welcome to CITY.'

    DECLARE @success int
    EXEC sp_OAMethod @email, 'SetReplacePattern', @success OUT, 'FIRST_NAME', 'John'
    EXEC sp_OAMethod @email, 'SetReplacePattern', @success OUT, 'CITY', 'Denver'

    DECLARE @n int
    EXEC sp_OAGetProperty @email, 'NumReplacePatterns', @n OUT
    DECLARE @i int

    SELECT @i = 0
    WHILE @i <= @n - 1
      BEGIN
        EXEC sp_OAMethod @email, 'GetReplacePattern', @sTmp0 OUT, @i

        EXEC sp_OAMethod @email, 'GetReplaceString', @sTmp1 OUT, @i
        PRINT @sTmp0 + ' -> ' + @sTmp1
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @email


END
GO