Peter
Mon Oct 30 05:48:21 CST 2006
> I have some code to do that stuff but I'd have to dig it out.
I don't have this to hand, but FWIW if you happen to be near a
German-language bookshop you can find this code on p. 331 of
Word-Programmierung: Das Handbuch - Entwicklung und Automatisierung mit VBA,
XML und VSTO (Meister, Gahler, Jamieson, Fressdorf), e.g. see
http://www.amazon.de/exec/obidos/ASIN/3860639897/qid=1147606171/sr=8-1/ref=sr_8_xs_ap_i1_xgl/028-1816925-4882945
Peter Jamieson
"Peter Jamieson" <pjj@KillmapSpjjnet.demon.co.uk> wrote in message
news:eLqkE$4%23GHA.3344@TK2MSFTNGP03.phx.gbl...
> FWIW, i agree with Doug. Although it's possible to create nested fields
> "on-the-fly", the starting conditions have to be exactly right (you have
> to be in such-and-such a view in Word, you have to have field code display
> switched on, hidden text displayed, and so on - off the top of my head I
> can't tell you exactly what you need: all I know is that it's not
> particularly easy to be sure that you get it right.
>
> Further, because "field code brace characters" are special, you cannot
> just specify a string of field code brace characters and ordinary text
> characters in a VBA string variable and use a method like InsertAfter to
> insert a set of nested fields. You can write a little interpreter to do it
> that makes it a bit easier, but at that point you are already moving away
> from the idea that you are "just" using VBA to create your Envelope:
> you're using VBA code and a separate, non-VBA specification of the fields,
> text and layout you want to create. For example, you can write VBA to take
> a string such as
>
> { IF ""{ MERGEFIELD abc }"" = """" ""A"" ""{ MERGEFIELD abc"" }
>
> and inser tit as a set of nested fields. To generalise it, of course you
> need to be able to distinguish between "field code braces" and ordinary
> "{" and "}" characters in your representation of the fields you want to
> insert. I have some code to do that stuff but I'd have to dig it out.
>
> However, once you've started down that path, you could for example do the
> following:
> a. create the envelope layout you need
> b. save it as RTF (or WordProcessingML format
> c. encode the entire RTF or XML as string data within your VBA code
> d. have your VBA
> - read the strings
> - write them out to a .rtf or .xml file
> - open the resulting document
> - create an envelope (or an AUTOTEXT, and from that, and envelope) from
> it
>
> It all depends on why you want to stick to "VBA only". If it's because you
> want to start without a separate file in a file system somewhere, the
> above approach avoids that. If it's because you want the user to be able
> to specify the envelope layout, and your code has therefore to construct a
> layout "on-the-fly", that's different: it's probably quite hard to
> customize rtf code or WordProcessingML code in that way.
>
> Just my 2c-worth...
>
> Peter Jamieson
>
>
> "Lüko Willms" <l.willms@domain.invalid> wrote in message
> news:czd2LKcn8EGd-pn2-CKmkslzkhp8N@lueko.willms.dialin.t-online.de...
>> Am Sat, 28 Oct 2006 18:44:18 UTC, schrieb "Doug Robbins - Word MVP"
>> <dkr@REMOVECAPSmvps.org> auf microsoft.public.word.vba.general :
>>
>>> .Fields.Add myrange, "First"
>>> myrange.End = mmdoc.Range.End
>>> myrange.InsertAfter " "
>>> myrange.End = mmdoc.Range.End
>>> myrange.Collapse wdCollapseEnd
>>> .Fields.Add myrange, "Middle"
>>> myrange.End = mmdoc.Range.End
>>> myrange.InsertAfter " "
>>
>> While fields can easily added this way, how about nested fields? I
>> would want to add a space after a first name only when there is a
>> first name, same for "middle", like this:
>>
>> {IF {MERGEFIELD Middle} <> "" "{MERGEFIELD Middle} " ""}
>>
>> How could I use .Fields.Add to create such nested fields? The above
>> one is still quite simple, the nesting could go deeper.
>>
>>
>> Yours,
>> L.W.
>>
>>
>>
>
>