I have been trying to resolve this an to no avail. I tried creating an instance of the objects and it is still not debugging correctly ...I would greatly appreciate any assistance possible...
The code I have entered is supposed to create a word table in word
with a heading above the table called Authors and Title and fill in the Word Table with the connection from the pubs database. It does not build unless I include lines 115 and 116. Then it will debug, create Authors and Table heading, and the first row of the Table Author:,Title and Royalty Pct. But an error message comes up saying "Object reference not set to an instance of an object." I tried many different combination's for the object creation but its still not working correctly....
using System;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
namespace SQLServerWordTable
{
public partial class ThisDocument
{
private void CreateWordTable()
{
Object start = 0;
Object end = 0;
Word.Range rng = Range(ref start, ref end);
rng.InsertBefore("Authors and Titles");
rng.Font.Name = "Verdana";
rng.Font.Size = 16;
rng.InsertParagraphAfter();
rng.InsertParagraphAfter();
rng.SetRange(rng.End, rng.End);
Object defaultTableBehavior = Type;
Object autoFitBehavior = Type;
rng.Tables.Add(
Paragraphs[2].Range,
1, 3, ref defaultTableBehavior, ref autoFitBehavior);
Word.Table tbl = Tables[1];
tbl.Range.Font.Size = 12;
tbl.Range.Font.Name = "Verdana";
tbl.Borders.InsideLineStyle =
Word.WdLineStyle.wdLineStyleSingle;
tbl.Borders.OutsideLineStyle =
Word.WdLineStyle.wdLineStyleDouble;
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";
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)
{
CreateWordTable();
String strSQL =
"SELECT au_lname, title, royaltyper " +
" FROM authors INNER JOIN titleauthor " +
" ON authors.au_id = titleauthor.au_id INNER JOIN titles " +
" titleauthor.title_id = titles.title_id ORDER BY au_lname";
SqlConnection cnn = null;
SqlDataReader sdr = null;
SqlCommand cmd = null;
try
{
cnn = new SqlConnection(
"Data Source=(local)\\SQLEXPRESS;Database=pubs;Workstation ID=DLDw5hdc1;Integrated Security=True");
cnn.Open();
cmd = new SqlCommand(strSQL, cnn);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (sdr != null)
{
sdr.Close();
}
}
int intRow = 2;
try
{
Word.Table tbl = Tables[1];
object beforeRow = this.Tables[1].Rows[1];
while(sdr.Read())
tbl.Rows.Add(ref beforeRow);
tbl.Cell(intRow, 1).Range.Text = sdr[0].ToString();
tbl.Cell(intRow, 2).Range.Text = sdr[1].ToString();
tbl.Cell(intRow, 3).Range.Text = sdr[2].ToString();
intRow += 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (sdr != null)
{
//sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
sdr.Close();
}
}
}
private void ThisDocument_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO Designer generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisDocument_Startup);
this.Shutdown += new System.EventHandler(ThisDocument_Shutdown);
}
#endregion
}
}