making tables in word? C# 2005

G

giddy

hi ,

i'm new to C# , i saw a piece of VB6 (i'm an ex vb6er) that runs word
and sends to it text and then a table. I want to achieve the same with
C# , i also need to control the formatting , some items in the table
should be bold , some coloured etc.. could someone point me to an
article or something??

also i have office 2007 on my pc now.. .i used to have 2003. But the
people who will use my app. have office Xp?My question is ,does
everything have to be different for different versions , if i write
code to send a table to word 2007 / 2003 , will the application even
run on a pc that has an older version or does'nt even have office ...
(since , i'm guessing , one has to refrence the word library or
something)

Thanks

Gideon
 
C

Cindy M.

Hi Giddy,
i'm new to C# , i saw a piece of VB6 (i'm an ex vb6er) that runs word
and sends to it text and then a table. I want to achieve the same with
C# , i also need to control the formatting , some items in the table
should be bold , some coloured etc.. could someone point me to an
article or something??
There's one here. It might not cover every question you have, but it
should give you a start:

http://msdn.microsoft.com/library/en-us/odc_wd2003_ta/html/OfficeWordAut
omatingTablesData.asp
Also i have office 2007 on my pc now.. .i used to have 2003. But the
people who will use my app. have office Xp?My question is ,does
everything have to be different for different versions , if i write
code to send a table to word 2007 / 2003 , will the application even
run on a pc that has an older version or does'nt even have office ...
(since , i'm guessing , one has to refrence the word library or
something)
There are two parts to this issue:

1. The code you compile has to reference a compatible object model
library (PIA). You can't develop against a newer object model library,
then install the compiled application for an older version. Office
application simply do NOT like that. Upward can work (won't necessarily,
but it can); backwards rarely does.

2. As long as you use parts of the object model that are available in
all versions of the object library, the code will work across multiple
versions. There have been additions to tables in each version, so you'd
need to be careful.

You can avoid (1) by using only late binding (PInvoke) - no reference to
an Office or Word object library. But if your new to C# that will be
more hassle than you may be ready for. With luck, you wouldn't run into
(2).

But ideally, you'd install Office XP on your development machine (with
no other version present). And you'd download the Office XP PIAs from
Microsoft.com and install them on your machine, and provide them as part
of your setup project. Unlike Office 2003 and 2007, there are no PIAs
distributed with Office XP, they have to be installed separately.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
G

giddy

hi ,
sorry i'm replying late , um i'm confused...

in VB6 i saw code like this :
Set wdApp = GetObject(, "Word.Application")
Set doc = wdApp.Documents.Add
......
......
'code to send table to word

and it worked fine... . i've seen the article you directed me to , when
i first did automation in vb , and i've seen a lot more msdn automation
articles including this one :
http://msdn.microsoft.com/library/d...l/wroriautomatingwordusingwordobjectmodel.asp

BUT whenever i run any of the code it says Microsoft.Office , the
namespace or lib does'nt exist??? why is that??

i also got download the latest VSTO package today , but nothings
changed .. ...????

i am new to C# , but i have good programming background , besides doing
Vb i did VC (pure C & win32api no mfc) , Java , flash , js etc.. so i
should have TOO MUCh of a hassle understanding stuff...

Thanks

from a very confused Gideon
 
G

giddy

oops sorry i did'nt refrence the dlls ! sorry. And i figured VSTO has
nothing to do with automation right? what is it then?

and ..

Cindy M: "you can avoid (1) by using only late binding (PInvoke) - no
reference to
an Office or Word object library."

how do i do that? could u plz show me an example or something!

Gideon
 
C

Cindy M.

Hi Giddy,
oops sorry i did'nt refrence the dlls ! sorry. And i figured VSTO has
nothing to do with automation right? what is it then?
Yes, that's exactly what I was going to say :)

VSTO is two things:
1. A way to associate managed code (.NET) with a document instead of
embedding "native" VBA. It also makes some tasks simpler by doing some
work in the background. More information here

http://msdn2.microsoft.com/en-us/library/23cw517s(VS.80).aspx

2. A more efficient way to create Office COM Add-ins (Shared Add-ins).
Here, again, some of the things are streamlined. And in addition it's
not necessary to create a Shim so that each runs in its own appdomain.

Automation, on the other hand, is simply manipulating the application
for a particular task, from within another application. Much of the
code used to work with an object model will be the same, whether you're
working with VSTO or with automation. The main differences will be the
objects at the top-most level of the hierarchy, where VSTO provides an
internal object (ThisAddin.application, for example)
Cindy M: "you can avoid (1) by using only late binding (PInvoke) - no
reference to
an Office or Word object library."

how do i do that? could u plz show me an example or something!
Here's an example
http://support.microsoft.com/kb/302901/

Look at the "Step-by-Step" for creating a COM Addin, the part about
building commandbars (Step 8) where it uses InvokeMember.

And here's another one:
http://support.microsoft.com/kb/303296/en-us

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
G

giddy

Hi Cindy ,

i was mistaken , my clients have office 2000! i coaxed them into
getting office 2003! So i dont need to worry about late binding for
now!

i ran the sample code you have on your article , it works perfect ,
BUT! when i tried to make my own little function it does'nt work ,
word fires up , the DATA shows up in word with no table borders! i have
word 2007 .. heres what it looks like ->
gidisrael.googlepages.com/wdAutomation.jpg

could you please tell me what i'm doing wrong , i've been studying your
code for days now and i still cant figure it out!

this is my code :

private void button1_Click(object sender, EventArgs e)
{

object objMissing = System.Reflection.Missing.Value;
object objTrue = true;
wd.ApplicationClass objWord = new wd.ApplicationClass();
objWord.Visible = true;
wd.Document objDoc = objWord.Documents.Add(ref objMissing,
ref objMissing,ref objMissing, ref objTrue);
wd.Range objRange = objDoc.Content;
wd.Table tbl = objDoc.Tables.Add(objRange, 4,4, ref
objDefaultBehaviorWord8, ref objAutoFitFixed);

int r = 1;
while (r<=4)
{

for (int nrCol = 1; nrCol <= 4; nrCol++)
{
// Now add the records.
tbl.Cell(r, nrCol).Range.Text =
Convert.ToString("Num" + nrCol);
}
r++;
}

}

Gideon
 
C

Cindy M.

<[email protected]>
<VA.00000813.018f4aa0@speedy>
<[email protected]>
Newsgroups: microsoft.public.office.developer.automation
NNTP-Posting-Host: 223.202.77.83.cust.bluewin.ch 83.77.202.223
Lines: 1
Path: number1.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newshub.sdsu.edu!msrtrans!TK2MSFTFEEDS01.phx.gbl!TK2MSFTFEEDS02.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSFTNGP02.phx.gbl
Xref: number1.nntp.dca.giganews.com microsoft.public.office.developer.automation:12515

Hi Giddy,

By default, borders won't usually be applied. From Word 2003 onwards, it
will depend on what Table Style the user may have selected as the default
(although most users never find that option). Whether or not gridlines
(which don't print) are applied depends on the setting of that option in
the Table menu.

So, you should apply any formatting you require to the table, after you
create it. (My article mainly focusses on getting data into tables.
Covering more than that could result in an entire book :)!)

You basically have three choices for formatting tables

1. Apply direct formatting. At first glance, this will seem the easiest
thing to do. But if your document contains more than one table, it will
be the slowest in the long run.

2. Use one of Word's built-in Table AutoFormat styles. You can look at
these through Table/AutoFormat. Note that you can select WHAT part of the
format is to be applied, and whether to apply it to the first and last
rows/columns. If you find one of these that does the job for you, record
applying it to a table in a macro to get the basic syntax.

3. Create your own Table Style. This will be the most difficult to code,
but give you the most flexibility combined with speed (vs. (1)).
I was mistaken , my clients have office 2000! i coaxed them into
getting office 2003! So i dont need to worry about late binding for
now!

i ran the sample code you have on your article , it works perfect ,
BUT! when i tried to make my own little function it does'nt work ,
word fires up , the DATA shows up in word with no table borders! i have
word 2007 .. heres what it looks like ->
gidisrael.googlepages.com/wdAutomation.jpg

could you please tell me what i'm doing wrong , i've been studying your
code for days now and i still cant figure it out!

this is my code :

private void button1_Click(object sender, EventArgs e)
{

object objMissing = System.Reflection.Missing.Value;
object objTrue = true;
wd.ApplicationClass objWord = new wd.ApplicationClass();
objWord.Visible = true;
wd.Document objDoc = objWord.Documents.Add(ref objMissing,
ref objMissing,ref objMissing, ref objTrue);
wd.Range objRange = objDoc.Content;
wd.Table tbl = objDoc.Tables.Add(objRange, 4,4, ref
objDefaultBehaviorWord8, ref objAutoFitFixed);

int r = 1;
while (r<=4)
{

for (int nrCol = 1; nrCol <= 4; nrCol++)
{
// Now add the records.
tbl.Cell(r, nrCol).Range.Text =
Convert.ToString("Num" + nrCol);
}
r++;
}

}

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
G

giddy

hi again! =S

i dont quite get what you mean by Apply direct formatting BUT i tried
option 2 and:
1. thw word macro says : Selection.Tables(1).Style = "Table Grid"

now the tables collection does not have a style property and i have to
set it via set_style() right? wel li tried and i get
COMexceptionUnhandled .... could not apply formatting! i tried putting
the line before filling the data then i tried it after I tried even
putting in objRange.Collapse(objCollapseEnd); , since you code has it ,
and i still get the same lousy error!

i dont think i need to worry about efficency , since most of the docs.
i need to send to word are small. BUT id just be happy(very very HAPPY)
to have succesfully sent a table into word! without any gliches!

I'm really sorry 'm bieng a nag! please answer this post only if you
want to.!


thanks so much for your reply earlier.

Gideon
 
C

Cindy M.

Hi Giddy,
i dont quite get what you mean by Apply direct formatting BUT i tried
option 2 and:
1. thw word macro says : Selection.Tables(1).Style = "Table Grid"

now the tables collection does not have a style property and i have to
set it via set_style() right? wel li tried and i get
COMexceptionUnhandled .... could not apply formatting! i tried putting
the line before filling the data then i tried it after I tried even
putting in objRange.Collapse(objCollapseEnd); , since you code has it ,
and i still get the same lousy error!

i dont think i need to worry about efficency , since most of the docs.
i need to send to word are small. BUT id just be happy(very very HAPPY)
to have succesfully sent a table into word! without any gliches!

I'm really sorry 'm bieng a nag! please answer this post only if you
want to.!
Yes, you need to use set_Style. But look at the method's signature
carefully: it says it requires an object of type: ref object. In this
particular case, it means the style name (a string) has to be cast to
type object. So:

//Picks up the table in which the cursor stands
wd.Table table = wdApp.Selection.Tables[1];
// casts the style name to type object
object tableStyle = "Table Colorful 1";
//applies the style to the table object
table.set_Style(ref tableStyle);


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top