none
Replace "&&" symbol with MailMerge symbol RRS feed

  • Question

  • I'm developing code using C# to read a word document.

    I want to replace all "&&" symbols in the document with "«" (Symbol of MailMerge)

    and all "^^" with "»" (closing symbol of MailMerge).

     

    I already tried using range object to replace all "&&" with "«". But the output came up with " (Double quotes).

    Can you tell me how by any means i can attain this..


    ~ Loveson
    Wednesday, December 14, 2011 3:23 PM

Answers

  • Hi Loveson

    Depending on which view options are activated in the Word interface, you'll see various representations of Word fields when you view a mail merge document in the Word window. This can be confusing to the "unitiated" :-)

    But Word manages dynamic content using "field codes". What you're seeing is a field result representation when the merge data view is not activated. Press Alt +F9 and you'll see the real field codes, which will look something like this: { Mergefield LastName }.

    The braces { } are not something you can type on the keyboard using the key with the brace symbol. They're a very special "thing" that contains a lot of information - that you can't see. They're called field braces (or brackets) and, in the UI, must be inserted by pressing Ctrl+F9. In the Word API the equivalent is the Document.Fields.Add method.

    Armed with this information, it becomes clear that this is not a simple case of Find/Replace, as it's not possible to use a field code as a replacement value.

    So, if I'm understanding correctly what you want to do, you'll need to use Range.Find with MatchWildcards activated to search this expression: (\<\<)(*)(\>\>)

    You can choose to do a Replace with the Replacement.Text: \2

    The result of this will be that the Range object will contain the text between your <<angled brackets>>.

    This will, I assume, become the name of the datafield to appear in the merge field. So you can assign this to a string object, to for the Name parameter of the Mergefields.Add method, and use the same Range object for the Range parameter of that method.

    Do this in a loop for the entire document...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Monday, December 26, 2011 6:32 AM
    Wednesday, December 14, 2011 4:01 PM
    Moderator
  • Hi Loveson,

    If you want to convert text strings bounded by particular characters to true field codes, see the vba code I've developed at: http://www.gmayor.com/export_field.htm#TextToField although the macro assumes the user will select the string to be converted, you could use a known range instead (or you could simply target the whole document). Also, the code assumes the text 'fields' will be bounded by '{' and '}', but you could easily change those to other characters or strings.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    • Marked as answer by Bruce Song Monday, December 26, 2011 6:32 AM
    Friday, December 16, 2011 6:54 AM

All replies

  • You denote those characters in your C# code sheet by casting ints. I think (which is to say, according to wikipedia) « is 111, so you'd write:

    (char)111

    I'm having difficulty finding the unicode number for the other symbol, because I don't have Word/Visual Studio on this computer. Basically you can do it the other way, cast a char as an int, and that was find out the integer representation (which will be unicode) of any charater in Word. It's easy enough to write a little app for that.

    Then you can use Word's Find object to find all the && or ^^ in your document, and alter them.

    string s;

    s[0] = (char)111;

    Range.Text = s;

     

    • Marked as answer by Bruce Song Monday, December 26, 2011 6:32 AM
    • Unmarked as answer by R Kipling Monday, December 26, 2011 9:09 AM
    Wednesday, December 14, 2011 3:47 PM
  • Hi Loveson

    Depending on which view options are activated in the Word interface, you'll see various representations of Word fields when you view a mail merge document in the Word window. This can be confusing to the "unitiated" :-)

    But Word manages dynamic content using "field codes". What you're seeing is a field result representation when the merge data view is not activated. Press Alt +F9 and you'll see the real field codes, which will look something like this: { Mergefield LastName }.

    The braces { } are not something you can type on the keyboard using the key with the brace symbol. They're a very special "thing" that contains a lot of information - that you can't see. They're called field braces (or brackets) and, in the UI, must be inserted by pressing Ctrl+F9. In the Word API the equivalent is the Document.Fields.Add method.

    Armed with this information, it becomes clear that this is not a simple case of Find/Replace, as it's not possible to use a field code as a replacement value.

    So, if I'm understanding correctly what you want to do, you'll need to use Range.Find with MatchWildcards activated to search this expression: (\<\<)(*)(\>\>)

    You can choose to do a Replace with the Replacement.Text: \2

    The result of this will be that the Range object will contain the text between your <<angled brackets>>.

    This will, I assume, become the name of the datafield to appear in the merge field. So you can assign this to a string object, to for the Name parameter of the Mergefields.Add method, and use the same Range object for the Range parameter of that method.

    Do this in a loop for the entire document...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Monday, December 26, 2011 6:32 AM
    Wednesday, December 14, 2011 4:01 PM
    Moderator
  • Hi Loveson,

    If you want to convert text strings bounded by particular characters to true field codes, see the vba code I've developed at: http://www.gmayor.com/export_field.htm#TextToField although the macro assumes the user will select the string to be converted, you could use a known range instead (or you could simply target the whole document). Also, the code assumes the text 'fields' will be bounded by '{' and '}', but you could easily change those to other characters or strings.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    • Marked as answer by Bruce Song Monday, December 26, 2011 6:32 AM
    Friday, December 16, 2011 6:54 AM
  • Sorry!..Tried it.

    But this did not work.


    ~ Loveson
    Monday, December 26, 2011 9:10 AM
  • Hi Can you post any sample code for this particular instance."»"

    Thanks


    ~ Loveson
    Monday, December 26, 2011 9:12 AM
  • Hi Loveson,

    I'm not sure what you want. If you're using « and/or » characters as your 'text' field delimiters, simply replace all of the corresponding { and/or } characters, respectively, in the code.

    If, however, you're expecting to end up with « and » characters as the final field markers, that's only possible if the fields are MERGEFIELDs, MERGESEQs, MERGESEQs, NEXTs, NEXTIFs and SKIPIFs. For such fields, the « and » characters appear automatically once the field codes are updated. Other kinds of fields don't display these characters when they're updated.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Monday, December 26, 2011 10:22 AM