LTSdinamico

Hello,

I am new to VSTO so any help will be greatly appreciated.

What I am trying to do is get information from a DB to a word document placing formatted labels to each record:

TEST ID: Test 100

TITLE: Test 101 sample bla bla bla

Bla bla bla.

Info: Test Info

<PAGE BREAK>

TEST ID: Test 101

TITLE: Test 101 sample bla bla bla

Bla bla bla.

Info: Test 101 Info

<PAGE BREAK>

¡­

So far I am been working of the following sample but I am unable to figure out how to format the labels without affecting the whole content and placing page breaks after each paragraph.

Walkthrough: Building a Word Document Using SQL Server Data

http://msdn2.microsoft.com/en-us/library/aa192487(office.11).aspx

Here is a sample of the code I am using:

cnn = new SqlConnection("Data Source=(local);Database=Test_DB;Integrated Security=true");

cnn.Open();

cmd = new SqlCommand(strSQL, cnn);

dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

Object item = "Data";

Word.Bookmark bmk = (Word.Bookmark)Globals.ThisDocument.Bookmarks.get_Item(ref item);

rng = bmk.Range;

while (dr.Read())

{

// rng.Text ="Test ID\t" + dr[0].ToString();

// rng.Font.Underline = Word.WdUnderline.wdUnderlineSingle;

sw.WriteLine("Test ID:\t{0}\r", dr[0]);

sw.WriteLine("Title:\t{0}\r", dr[1]);

sw.WriteLine("Info1 :\t{0}\r", dr[2]);

}

rng.Text = sw.ToString();

rng.Font.Name = "Verdana";

rng.Font.Size = 10;

....

THANKS!




Re: Visual Studio Tools for Office SQL to Word Sample

Cindy Meister

You're on the right track :-) You're just missing one little piece to the puzzle...

When you think about working with ranges, picture a selection. Except the range is a "selection" that's not visible on the screen. When applying formatting as a user, you need to make different selections in order to apply different formatting. The same is true when working with ranges.

So you start with your the range where you're writing the label:

rng.Text ="Test ID\t" + dr[0].ToString();

rng.Font.Underline = Word.WdUnderline.wdUnderlineSingle;

At this point, you need to collapse the range to a single point (a blinking cursor if you were an end-user):

object objCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;

rng.Collapse(ref objCollapseEnd);

Now you can start with the rest of the range:

rng.Text = sw.ToString();

rng.Font.Name = "Verdana";

rng.Font.Size = 10;

And when you're finished, collapse it again to insert the page break, etc.






Re: Visual Studio Tools for Office SQL to Word Sample

LTSdinamico

Thanks Cindy,
I made some changes to the code and now is working =]
The code now looks as follow:

rng.Text ="Test ID:" ;

rng.Font.Underline = Word.WdUnderline.wdUnderlineSingle;

rng.Font.Bold = 1;

rng.Font.Name = "Verdana";

rng.Font.Size = 10;

object objCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;

rng.Collapse(ref objCollapseEnd);

rng.Text = "\t" + dr[0].ToString();

rng.Font.Underline = Word.WdUnderline.wdUnderlineNone;

rng.Font.Bold = 0;

rng.InsertParagraphAfter();

rng.InsertParagraphAfter();

rng.Collapse(ref objCollapseEnd);

rng.Text = "Title:";

rng.Font.Underline = Word.WdUnderline.wdUnderlineSingle;

rng.Font.Bold = 1;

rng.Font.Name = "Verdana";

rng.Font.Size = 10;

rng.Collapse(ref objCollapseEnd);

rng.Text = "\t" + dr[1].ToString();

rng.Font.Underline = Word.WdUnderline.wdUnderlineNone;

rng.Font.Bold = 0;

rng.InsertParagraphAfter();

rng.InsertParagraphAfter();

rng.Collapse(ref objCollapseEnd);