An object reference is required for the non-static field, method, or property....
Below code returns:
An object reference is required for the non-static field, method, or property 'Microsoft.Office.Tools.Word.Document.Tables.get' Would appreciate any help.. Scroll about midway down and have made comments where the error is. The way to undo all the scrabble is just click edit and it will then display correctly.
Thanks,
Jim
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
namespace SQLServerWordTable
{
public partial class ThisDocument
{
int[] rng = {1, 2, 3};
private void CreateWordTable()
{
// Move to start of document.
Object start = 0;
Object end = 0;
Word.Range rng = Range(ref start, ref end);
// Insert Title and paragraph marks.
rng.InsertBefore("Authors and Titles");
rng.Font.Name = "Verdana";
rng.Font.Size = 16;
rng.InsertParagraphAfter();
rng.InsertParagraphAfter();
rng.SetRange(rng.End, rng.End);
// Add the table.
Object defaultTableBehavior = System.Type.Missing;
Object autoFitBehavior = System.Type.Missing;
rng.Tables.Add(
Paragraphs[2].Range, 1, 3, ref defaultTableBehavior, ref autoFitBehavior);
// Set object variable to point to new table.
Array Tables;
//Below line is where I am having trouble..error message is
//An object reference is required for the non-static field, method, or //property 'Microsoft.Office.Tools.Word.Document.Tables.get'
//when I debug..
Word.Table tbl = ThisDocument.Tables[1];
//Word.Table tbl = rng.Tables[1];
//Word.Table tbl = new Microsoft.Office.Interop.Word.Tables[1];
// Format the table.
tbl.Range.Font.Size = 12;
tbl.Range.Font.Name = "Verdana";
tbl.Borders.InsideLineStyle =
Word.WdLineStyle.wdLineStyleSingle;
tbl.Borders.OutsideLineStyle =
Word.WdLineStyle.wdLineStyleDouble;
// Set the column widths.
tbl.Columns[1].SetWidth(
ThisApplication.InchesToPoints((float)1.5),
Word.WdRulerStyle.wdAdjustNone);
tbl.Columns[2].SetWidth(
ThisApplication.InchesToPoints((float)3.25),
Word.WdRulerStyle.wdAdjustNone);
tbl.Columns[3].SetWidth(
ThisApplication.InchesToPoints((float)1.25),
Word.WdRulerStyle.wdAdjustNone);
tbl.Cell(1, 1).Range.Text = "Author";
tbl.Cell(1, 2).Range.Text = "Title";
// Right-align third column.
Word.Range rngCell = tbl.Cell(1, 3).Range;
rngCell.Text = "Royalty Pct";
rngCell.ParagraphFormat.Alignment =
Word.WdParagraphAlignment.wdAlignParagraphRight;
}
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
// C#
CreateWordTable();
}
private void ThisDocument_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisDocument_Startup);
this.Shutdown += new System.EventHandler(ThisDocument_Shutdown);
}
}
}