C# Interlop字:表行计数不正确

C# Interlop字:表行计数不正确,c#,ms-word,C#,Ms Word,我试图从800多个具有相同结构的word文件中收集信息。在头条部分有一张桌子。我需要的是桌上的东西。该表有4行,但当我尝试获取行计数时,它返回1 Word.Application m_word =new Microsoft.Office.Interop.Word.Application(); Word.Document doc; Object filename = treeList1.FocusedNode["SOPName"].ToStrin

我试图从800多个具有相同结构的word文件中收集信息。在头条部分有一张桌子。我需要的是桌上的东西。该表有4行,但当我尝试获取行计数时,它返回1

        Word.Application m_word =new  Microsoft.Office.Interop.Word.Application();
        Word.Document doc;

        Object filename = treeList1.FocusedNode["SOPName"].ToString();
        Object filefullname = treeList1.FocusedNode["FullPath"].ToString();
        Object confirmConversions = Type.Missing;
        Object readOnly = Type.Missing;
        Object addToRecentFiles = Type.Missing;
        Object passwordDocument = Type.Missing;
        Object passwordTemplate = Type.Missing;
        Object revert = Type.Missing;
        Object writePasswordDocument = Type.Missing;
        Object writePasswordTemplate = Type.Missing;
        Object format = Type.Missing;
        Object encoding = Type.Missing;
        Object visible = Type.Missing;
        Object openConflictDocument = Type.Missing;
        Object openAndRepair = Type.Missing;
        Object documentDirection = Type.Missing;
        Object noEncodingDialog = Type.Missing;

       doc= m_word.Documents.Open(ref filefullname,
                   ref confirmConversions, ref readOnly, ref addToRecentFiles,
                   ref passwordDocument, ref passwordTemplate, ref revert,
                   ref writePasswordDocument, ref writePasswordTemplate,
                   ref format, ref encoding, ref visible, ref openConflictDocument,
                   ref openAndRepair, ref documentDirection, ref noEncodingDialog
                   );
        m_word.Visible =true;
        string msg=string.Empty;

        Word.Table InfoTable =
            doc.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Tables[1];
        msg += string.Format("The table has {0} rows,{1} columns.\n", InfoTable.Rows.Count, InfoTable.Columns.Count);
        for (int i = 1; i <= InfoTable.Rows.Count; i++)
            for (int j = 1; j <= InfoTable.Columns.Count; j++)
                msg += string.Format("{0},{1}:{2}\n", i, j, InfoTable.Cell(i, j).Range.Text);
        MessageBox.Show(msg);
Word.Application m_Word=new Microsoft.Office.Interop.Word.Application();
Word文档文档;
对象文件名=treeList1.FocusedNode[“SOPName”].ToString();
对象filefullname=treeList1.FocusedNode[“FullPath”].ToString();
Object confirmConversions=类型。缺少;
对象只读=类型。缺少;
Object addToRecentFiles=类型。缺少;
对象passwordDocument=类型。缺少;
对象passwordTemplate=类型。缺少;
对象还原=类型。缺少;
对象writePasswordDocument=类型。缺少;
Object writePasswordTemplate=类型。缺少;
对象格式=类型。缺少;
对象编码=类型。缺少;
对象可见=类型。缺少;
对象openConflictDocument=类型。缺少;
Object openAndRepair=类型。缺少;
对象documentDirection=类型。缺少;
Object noEncodingDialog=类型。缺少;
doc=m_word.Documents.Open(参考filefullname,
ref确认转换、ref只读、ref addToRecentFiles、,
ref passwordDocument、ref passwordTemplate、ref revert、,
ref writePasswordDocument,ref writePasswordTemplate,
ref格式、ref编码、ref可见、ref openConflictDocument、,
ref openAndRepair、ref documentDirection、ref noencoding对话框
);
m_word.Visible=true;
string msg=string.Empty;
Word.Table信息表=
文档节[1]。标题[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]。范围。表格[1];
msg+=string.Format(“该表有{0}行、{1}列。\n”、InfoTable.rows.Count、InfoTable.columns.Count);

对于(int i=1;我尝试从Microsoft安装OpenXML生产力工具。这将允许您检查文档的组成(并且它可以显示从C#生成文档的代码)。您可能会发现
doc.Sections[1]。Headers[Word.wdheaderfooteridex.wdheaderfooterprismary].Range.Tables[1];
不是参考您需要的表格的正确方法。非常感谢。您的建议非常有用。我现在就知道了。