C# 尝试在C中的表单元格上写入时出错#

C# 尝试在C中的表单元格上写入时出错#,c#,word,word-interop,C#,Word,Word Interop,我正试图在Visual Studio C的帮助下创建一个自定义Word文档,其中包含一个表和一些文本 这是我的密码 object oMissing = System.Reflection.Missing.Value; object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */ //Start Word and create a new document. Word._Applicatio

我正试图在Visual Studio C的帮助下创建一个自定义Word文档,其中包含一个表和一些文本

这是我的密码

object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);


Word.Table newTable;
Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
newTable = oDoc.Tables.Add(wrdRng, 1, 3, ref oMissing, ref oMissing);
newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
// newTable.AllowAutoFit = true;

// Formattazione prima tabella
newTable.Cell(0, 1).Range.Text = "Olivero SRL";
newTable.Cell(0, 2).Range.Text = "PSQ/18/01/A8";
newTable.Cell(0, 3).Range.Text = "Vers. 0.1";
        

newTable.Cell(0, 1).Range.FormattedText.Font.Size = 14;
newTable.Cell(0, 1).Range.FormattedText.Bold = 1;
newTable.Cell(0, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

newTable.Cell(0, 2).Range.FormattedText.Font.Size = 14;
newTable.Cell(0, 2).Range.FormattedText.Bold = 1;
newTable.Cell(0, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

newTable.Cell(0, 3).Range.FormattedText.Font.Size = 14;
newTable.Cell(0, 3).Range.FormattedText.Bold = 1;
这是创建Word文档的经典方法,由MSDN提供
当涉及到这一行时,
newTable.Cell(0,1).Range.Text=“Olivero SRL”因此,当我尝试在第一个表格单元格中插入一些值时,我得到以下错误

远程过程调用失败。(来自HRESULT的异常:0x800706BE)

在互联网上搜索时,我只找到了一般的修复方法,但与c#或编码相比,没有找到任何修复方法。

单词单元格(行和列)索引编号将从1开始,而不是从0开始。 你可以从中看到一个例子

因此,请将代码更改为:

newTable.Cell(1, 1).Range.Text = "Olivero SRL";
newTable.Cell(1, 2).Range.Text = "PSQ/18/01/A8";
newTable.Cell(1, 3).Range.Text = "Vers. 0.1";
... rest of the code
单词单元格(行和列)索引编号将从1开始,而不是从0开始。 你可以从中看到一个例子

因此,请将代码更改为:

newTable.Cell(1, 1).Range.Text = "Olivero SRL";
newTable.Cell(1, 2).Range.Text = "PSQ/18/01/A8";
newTable.Cell(1, 3).Range.Text = "Vers. 0.1";
... rest of the code