Word Automation (VSTO): Copy style from a table to an other

Hello,

I'm using Visual Studio 2005 and VSTO to generate some RTF documents by Word automation.

In some cases, an input template document is available, containing a table and some text.
I'd like to add a new table to this document, and copy style (borders and font properties) from existing table to this new table.

First, I tried following code but it didn't work:
object laststyle = wordDoc.Tables[wordDoc.Tables.Count - 1].get_Style();
newtable.set_Style(ref laststyle);

Then I used attached code to copy property by property, but it's complicated.

Would you have any other idea for copying the style of a table to a an other?

Thanks,
Julien
// Go to the end of the document
            wordDoc.Activate();
            object unit = Word.WdUnits.wdStory;
            wordApp.Selection.EndKey(ref unit, ref missing);
            lasttable = wordDoc.Tables.Add(wordApp.Selection.Range, nrow, ncol, ref missing, ref missing);
            if (wordDoc.Tables.Count > 1)
            {
                //object laststyle = wordDoc.Tables[wordDoc.Tables.Count - 1].get_Style();
                //lasttable.set_Style(ref laststyle);
                Word.Table reftable = wordDoc.Tables[wordDoc.Tables.Count - 1];
                lasttable.Borders.InsideLineStyle = reftable.Borders.InsideLineStyle;
                
                lasttable.Borders.OutsideLineStyle = reftable.Borders.OutsideLineStyle;
                if (reftable.Borders.OutsideLineStyle != Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone)
                {
                    lasttable.Borders.OutsideLineWidth = reftable.Borders.OutsideLineWidth;
                    lasttable.Borders.OutsideColor = reftable.Borders.OutsideColor;
                }

                // If there is no inside line defined, create one
                if (lasttable.Borders.InsideLineStyle == Word.WdLineStyle.wdLineStyleNone)
                {
                    // Copy inside line style from outside
                    if (lasttable.Borders.OutsideLineStyle != Word.WdLineStyle.wdLineStyleNone)
                    {
                        lasttable.Borders.InsideLineStyle = lasttable.Borders.OutsideLineStyle;
                        lasttable.Borders.InsideLineWidth = lasttable.Borders.OutsideLineWidth;
                        lasttable.Borders.InsideColor = reftable.Borders.OutsideColor;
                    }
                    else
                    {
                        lasttable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                        lasttable.Borders.InsideLineWidth = Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth025pt;
                        lasttable.Borders.InsideColor =Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;
                    }
                }
                else
                {
                    lasttable.Borders.InsideLineStyle = reftable.Borders.InsideLineStyle;
                    lasttable.Borders.InsideLineWidth = reftable.Borders.InsideLineWidth;
                    lasttable.Borders.InsideColor = reftable.Borders.InsideColor;
                }

                
                lasttable.Range.Font = reftable.Range.Font;
            }

Open in new window

LVL 2
JulienVanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

xenacodeCommented:
Hi,

Unfortunately copying properties individually as you have done in the code above is the best way. You didn't say if you were using Office 2003 or 2007 and there are differences between the two versions. In 2007 you can apply a table Style like this:

table1.Style = table2.Style

but this will only copy the properties that are included in a built-in table style. The other type of Style Property is one that you apply to a Selection and affects font, line spacing etc. Neither type of Style incorporates all the properties of a table and I don't believe there is a simple way to do this.

Pete
Xenacode Ltd
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JulienVanAuthor Commented:
Hi Pete, thanks for your comment.
I'm using Office 2007 but the Style property doesn't exist for my tables.
OK so I'll continue to copy property by property.

Julien
0
xenacodeCommented:
Not sure what you mean by that. The Class Microsoft.Office.Interop.Word.Table has a Style property which you can use to copy built-in styles fro mone table to another.

Are you using the Office 2007 Primary Interop Assemblies and VSTO 2005 Second Edition?

Pete
0
JulienVanAuthor Commented:
Yes I'm using VSTO 2005 Second Edition with a reference to the Microsoft Word 12.0 Object library (Microsoft.Office.Interop.Word) in my project.
As you can see on the atteched screen capture, the Style property doesn't exist for the Word.Table object in my case.
word.table.jpg
0
xenacodeCommented:
That's odd.

Did you create this project from one of the Visual Studio Installed Templates i.e. from the Project Types list: Office - 2003 Add-ins - Word Add-in?

If so, did you remove and re-create the reference to the Word interop at any point?

If Yes to the first question and No to the second, I'd suggest re-installing the Office 2007 PIA.

Pete
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.