none
Carriage Returns in OpenXml and Office 2003 RRS feed

  • Question

  • Okay, so from what I read on Microsoft's site, the OpenXml stuff is supposed to be compatible with previous versions of Office, back to 2000 I believe (at least XP and 2003).

    So, I have a PowerPoint automation and where it is failing is with 2003 Office with handling Carriage Returns in text fields (i.e., the innerxml, etc.).  What it does is takes Carriage Returns and/or Outlines and turns them into bad characters (comes up as a question mark inside a small box, etc.).

    So, any ideas?  Is there a way to test for the version of Office and fix on the fly, etc.?  Does OpenXml automation work on Office 2003 or not?

    And, yes, I'm aware of the "Compatibility Pack", but this will not do.  We can't just go and install yet another program on everybody's machine.  Is the a coding way to handle this?

    Update - the Computers this is happening on do already have the Compatibility Pack installed, so this pack does not actually make things compatibile.

    Friday, March 18, 2011 3:29 PM

Answers

  • Okay, so I figured out a solution.  Basically, Office 2003 can't parse text values with Carriage Returns.  So, you can split up a text with Carriage Returns by the CRs then individually make paragraph runs and insert into the appropriate parent nodes on the slide.

    private string[] GetMultiLines(string MultiLineText)
    {
       string[] split = new string[] {"\r\n"};
       string[] result = MultiLineText.Split(split, StringSplitOptions.RemoveEmptyEntries);
       return result;
    }

    ///Use string array from above and individually add paragraph nodes to the PPT text area
    public void UpdateMultiLine(Dictionary<string, string> Schemas, OpenXmlPart Part, string SearchString, string[] UpdatedText)
    {
       NameTable nt = new NameTable();
       XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);

       foreach (KeyValuePair<string, string> entry in Schemas)
       {
          nsManager.AddNamespace(entry.Key.ToString(), entry.Value.ToString());
       }
      
       try
       {
          XmlDocument xmldoc = new XmlDocument(nt);
          xmldoc.Load(Part.GetStream());
          XmlNode xNode = xmldoc.SelectSingleNode(SearchString, nsManager);
          XmlNode parNode = xNode.ParentNode.ParentNode.ParentNode;
          foreach (string s in UpdatedText)
          {
             if (string.Equals(s,UpdatedText.First<string>()))
             xNode.InnerText = s;
             else
             {
                XmlNode newNode = xNode.ParentNode.ParentNode.CloneNode(true);
                newNode.SelectSingleNode("//a:r/a:t", nsManager).InnerText = s;
                parNode.AppendChild(newNode);
             }
          }
         
          xmldoc.Save(Part.GetStream());
       }
       catch (Exception ex)
       {
          throw ex.InnerException;
       }
    }

    • Marked as answer by sdfsda Sunday, March 20, 2011 4:50 PM
    Sunday, March 20, 2011 4:49 PM

All replies

  • Office 2007 was the first version that used the XML standard.  So no, it won't work on version earlier than that.
    Friday, March 18, 2011 10:58 PM
  • Okay, so I figured out a solution.  Basically, Office 2003 can't parse text values with Carriage Returns.  So, you can split up a text with Carriage Returns by the CRs then individually make paragraph runs and insert into the appropriate parent nodes on the slide.

    private string[] GetMultiLines(string MultiLineText)
    {
       string[] split = new string[] {"\r\n"};
       string[] result = MultiLineText.Split(split, StringSplitOptions.RemoveEmptyEntries);
       return result;
    }

    ///Use string array from above and individually add paragraph nodes to the PPT text area
    public void UpdateMultiLine(Dictionary<string, string> Schemas, OpenXmlPart Part, string SearchString, string[] UpdatedText)
    {
       NameTable nt = new NameTable();
       XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);

       foreach (KeyValuePair<string, string> entry in Schemas)
       {
          nsManager.AddNamespace(entry.Key.ToString(), entry.Value.ToString());
       }
      
       try
       {
          XmlDocument xmldoc = new XmlDocument(nt);
          xmldoc.Load(Part.GetStream());
          XmlNode xNode = xmldoc.SelectSingleNode(SearchString, nsManager);
          XmlNode parNode = xNode.ParentNode.ParentNode.ParentNode;
          foreach (string s in UpdatedText)
          {
             if (string.Equals(s,UpdatedText.First<string>()))
             xNode.InnerText = s;
             else
             {
                XmlNode newNode = xNode.ParentNode.ParentNode.CloneNode(true);
                newNode.SelectSingleNode("//a:r/a:t", nsManager).InnerText = s;
                parNode.AppendChild(newNode);
             }
          }
         
          xmldoc.Save(Part.GetStream());
       }
       catch (Exception ex)
       {
          throw ex.InnerException;
       }
    }

    • Marked as answer by sdfsda Sunday, March 20, 2011 4:50 PM
    Sunday, March 20, 2011 4:49 PM