I'm trying to create a custom Word document with a table inside and some text with the help of Visual Studio C#
Here is my code.
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;
This is a classic way of creating a Word document, provided by MSDN.
When It comes to this line newTable.Cell(0, 1).Range.Text = "Olivero SRL";
So when I try to insert some value inside my 1st table cell I get the following error
The remote procedure call failed. (Exception from HRESULT: 0x800706BE)
Searching around the internet I find only general fixes but nothin compared to c# or coding.
The Word cells (rows and columns) index number will start from 1 rather then 0. You can see an example in this MSDN article.
So change your code to:
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
User contributions licensed under CC BY-SA 3.0