如何通过 C# 在 word 文档 table 中添加行
How to Add row in word document table by C#
在 word 文档中添加新行时 table 我遇到了下一个错误:-
System.Runtime.InteropServices.COMException: 'The requested member of
the collection does not exist.'
我使用的完整代码:-
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
object miss = System.Reflection.Missing.Value;
object path = string.Format(@"Doc_Path_here");
object readOnly = false;
Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
doc.Tables[0].Rows.Add();
我尝试了现有 here and 的解决方案,但没有任何积极的结果。
感谢任何帮助。
更新:-
word文档有一个table,这个确认了,
并且下一个代码的结果 nCount
变量是 1
int nCount = doc.Tables.Count;
如所述here in MSDN:
The "requested member of the collection does not exist" error occurs when you try to access an object that does not exist.
您应该在尝试访问之前检查成员是否存在。您可以使用 collection 的 Count
属性 来确定该成员是否存在。
正如@Cindy Meister 在评论中提到的,Office collection 不是基于零的。尝试像这样访问它:
doc.Tables[1].Rows.Add();
在 word 文档中添加新行时 table 我遇到了下一个错误:-
System.Runtime.InteropServices.COMException: 'The requested member of the collection does not exist.'
我使用的完整代码:-
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
object miss = System.Reflection.Missing.Value;
object path = string.Format(@"Doc_Path_here");
object readOnly = false;
Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
doc.Tables[0].Rows.Add();
我尝试了现有 here and
感谢任何帮助。
更新:-
word文档有一个table,这个确认了,
并且下一个代码的结果 nCount
变量是 1
int nCount = doc.Tables.Count;
如所述here in MSDN:
The "requested member of the collection does not exist" error occurs when you try to access an object that does not exist.
您应该在尝试访问之前检查成员是否存在。您可以使用 collection 的 Count
属性 来确定该成员是否存在。
正如@Cindy Meister 在评论中提到的,Office collection 不是基于零的。尝试像这样访问它:
doc.Tables[1].Rows.Add();