none
OpenXML tables not being read in MS word

    Question

  • I'm trying to read all the tables from a word file into a list, although for some reason the count is 0 regardless of how many tables are in the file. Here's my code.

        public void FindAndReplace(string DocPath)
        {
          using (WordprocessingDocument doc = WordprocessingDocument.Open(DocPath, true))
          {
            using (StreamReader reader = new StreamReader(doc.MainDocumentPart.GetStream()))
            {
              List<Table> tables = doc.MainDocumentPart.Document.Descendants<Table>().ToList();
              System.Console.WriteLine(tables.Count);

    tables.Count = 0. What am I doing wrong?

    vendredi 4 mai 2018 22:21

Réponses

  • Problem solved. I was not aware of the difference between DocumentFormat.OpenXml.Drawing and DocumentFormat.OpenXml.Wordprocessing. I was using the former because that's what it suggested I should use as default, but now that I changed it over to wordprocessing it works.
    • Marqué comme réponse emsimpson92 mercredi 9 mai 2018 15:10
    mercredi 9 mai 2018 15:10

Toutes les réponses

  • Hi emsimpson92,

    According to your question is more related to Open XML Format SDK, I will move the thread to ASP.NET forum for better support.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    lundi 7 mai 2018 06:36
  • Hello emsimpson92,

    I tried to use your code for testing and I did not reproduce your issue. It do return the correct tables count for me.

    I'm using OpenXML SDK 2.5 and Office 2016(9226.2114) for testing, what's your environment?

    Will this issue occur on specific document or all the documents in your PC? If it is related to specific document, please share a sample document for testing.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    mardi 8 mai 2018 01:59
    Modérateur
  • It happens with any document. I'm using OpenXML 2.8.1 and microsoft office 2016. Maybe it has something to do with my implementation?

    Right now my find and replace method is within a separate class, which also inherits from an interface. In my main, I create an instance of that class, as an interface, then I call find and replace. For example:

    List<MyInterface> myList = new List<MyInterface>();

    MyInterface foo = new MyClassA();

    MyInterface foo1 = new MyClassB();

    MyInterface foo2 = new MyClassC();

    myList.Add(foo);

    myList.Add(foo1);

    myList.Add(foo2);

    foreach(MyInterface i in myList)

    {

    i.FindAndReplace(bar);

    }

    I declared it as an interface because I have multiple classes that I want to run their own specialized FindAndReplace on.

    Edit: Just tested with version 2.5. Still returns a 0 count. Running step by step in the debugger provides no valuable information.



    mardi 8 mai 2018 18:15
  • Hello emsimpson92,

    I could not reproduce your design according to your description. I would suggest you create a simply project and share it to us so we could us it to reproduce your issue.

    For sharing the project, you could share it via cloud storage, such as One Drive, and then put link address here.

    Thanks for understanding,

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    mercredi 9 mai 2018 08:45
    Modérateur
  • Problem solved. I was not aware of the difference between DocumentFormat.OpenXml.Drawing and DocumentFormat.OpenXml.Wordprocessing. I was using the former because that's what it suggested I should use as default, but now that I changed it over to wordprocessing it works.
    • Marqué comme réponse emsimpson92 mercredi 9 mai 2018 15:10
    mercredi 9 mai 2018 15:10