Home All Groups Group Topic Archive Search About

Write text to File with template

Author
2 Jul 2009 8:11 AM
OndrejD
Hello,
I have some template text file.
I need write more text files, that contains text from template and have fill
in other text (for example adress).
In MS Word it is "Correspondence".
Can you help me, haw can I write it in VB?

Thank You
OndrejD

Author
2 Jul 2009 2:07 PM
Jeremiah D. Seitz
On Thu, 2 Jul 2009 01:11:01 -0700, OndrejD
<Ondr***@discussions.microsoft.com> wrote:

>Hello,
>I have some template text file.
>I need write more text files, that contains text from template and have fill
>in other text (for example adress).
>In MS Word it is "Correspondence".
>Can you help me, haw can I write it in VB?

Q&D, but you should get the idea.

Template:
    Name : {name}
    Address : {address}
    etc ...

Get the template data :
F = FreeFile
Open "Template.dat" For Binary As #F
Buff = Space$(Lof(F))
Get#F, 1, Buff
Close #F

Populate the template :
If Instr(Buff, "{name}") <> 0 Then
    Buff = Replace$(Buff, "{name}", mName)
End If
If Instr(Buff, "{address}") <> 0 Then
    Buff = Replace$(Buff, "{address}", mAddress)
End If
etc ...

Display:
txtOutput.Text = Buff

You can extend the logic to use an RTB or HTML. If you're talking
about using Word files, that's an entirely different animal, although
the concept may still apply.

Essentially, you define tokens that have a specific meaning (eg.:
{name}), and then replace the occurrences of the tokens with their
actual values. I personally tend to use {} to wrap my tokens, because
they are seldom used in the wild, reducing the possibility of a
literal being interpreted as a token.

I can explain better if necessary, but I'm still on my first coffee
.... ugh.

>Thank You
>OndrejD

    HTH,

    J.
    Jeremiah D. Seitz
    Omega Techware
    http://www.omegatechware.net