c# question..
I am having problem with the below code. I am trying to create a table in word and it is not
letting me because of the ThisDocument error. Is
there anyone who might be able to lend some help.
It would be greatly appreciated. Thanks in advance.
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;
//The below line is what
// is giving me a problem..the error is
// An object reference is reguired for
//the non-static field,
// method, or property 'Microsoft.Office.
//Tools.Word.Document.Tables.get'
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);
}
#endregion
}
}
Danatas GerviPosted Nov 1, 2009, 1:52 AM
I have got your mail.
Send you c# code file to my e-mail, I will format it for forum.
:-)
Danatas GerviPosted Nov 5, 2009, 2:06 AM
added to ThisDocument. Are you sure, that tis array of tables is not empty?
Danatas GerviPosted Nov 5, 2009, 2:00 AM
using System;
using System.Data;
using System.Drawing;
using Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
namespace SQLServerWordTable
{
public partial class ThisDocumnet
{
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.End0;
//Add the Table `
Object defualtTableBehavior = System.Type.Missing;
Object defualtFitBehavior = System.Type.Missing;
rng.Tables.Add( Paragraphs[2].Range, 1, 3, ref defaultTableBehavior, ref autoFitBehavior);
//Set object variable to point to new table.
//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];
// 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)
{
}
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisDocument_Startup);
this.Shutdown += new System.EventHandler(ThisDocument_Shutdown);
}
}
}
Danatas GerviPosted Oct 28, 2009, 4:38 AM
If you want to be helped, please,
Copy all yor code into an Word document (ctrl+A, ctrl+c - in Visual studio, ctrl+V - in Word)
set the font size to 9 (for all code that was copied) select it again in Word, copy to clip board, and paste again to forum.
I tryed to decode - insert manually all tabs and new line separators - but i fails, because of your commented rows of code - how can I understand, what is really commented, what is not? I have got all as one long string....
Thank you.