none
Word 2007 ProofErr Issue RRS feed

  • Question

  •  

    Here is what I am trying to do:

     

    - I am attempting to modify the document XML of a Word 2007 file.

    - I read the XML into a String using C#

    - I want to do a Replace on the string, swapping out custom identifier words (like #Amount#) in the doc, with data from SQL. (So that #Amount# will be replaced with 1234.56, a pseudo mail-merge)

    - My Replace is failing because my custom words are being broken up by Word 2007. It is adding w: proofErr tags into my XML, sometimes in the middle of my custom words.

     

    Example:

     

    I have #Amount# in my Word doc. When my code tries to find #Amount#, it instead finds something like:

     

    <w: proofErr w:type="spellStart"/>

    <w:r>

    <w:t>#</w:t>

    </w:r><w: proofErr w:type="spellEnd"/>

    <w:r>

    <w:t>Amount</w:t>

    </w:r>

     

    Is there any way to have Word avoid doing this? Thanks!

     

    (Also, typing ":" then "p" makes a smiley, so I put a space between them in the XML)

    Tuesday, February 12, 2008 6:31 PM

All replies

  • Hi,

    Yes, it is possible. It adds the word "Amount" to the dictionary of Word

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Hola,

    Sí, se puede. Añade la palabra "Amount" al diccionario de Word.

    • Proposed as answer by Pedro Hueso Tuesday, January 27, 2009 4:23 PM
    • Edited by Pedro Hueso Tuesday, January 27, 2009 4:32 PM Mal traducido
    Tuesday, January 27, 2009 4:17 PM
  • It's probably safer and easier if you use content controls instead of placeholder text.
    Zeyad Rajabi (MS)
    Wednesday, January 28, 2009 2:58 AM
  • If all you want to do is replace every occurrence of #Amount# with someValue try this code:

     


    Regex regex = new Regex(searchRegexp, RegexOptions.Multiline | RegexOptions.IgnoreCase); 
     
    using (Package package = Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite)) 
     { 
         foreach (PackagePart part in package.GetParts()) 
         { 
         if (part.Uri.ToString().IndexOf(".xml") != -1 
         || 
         part.Uri.ToString().IndexOf(".rels") != -1) 
         { 
             System.IO.FileStream img= (FileStream) part.GetStream(); 
             XmlDocument document = new XmlDocument(); 
     

             document.Load(part.GetStream()); 
                
                 if (regex.IsMatch(document.InnerXml)) 
                 { 

                  //replacement is string that you can change depending on what you want to

                  //put instead of searchRegexp

                 document.InnerXml = regex.Replace(document.InnerXml, replacement); 
     
                 document.Save(part.GetStream(FileMode.Create, FileAccess.Write)); 
                 } 
         } 
         }
     }
    Wednesday, January 28, 2009 3:34 PM