JulienVan
asked on
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.Tab les.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
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.Tab
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Not sure what you mean by that. The Class Microsoft.Office.Interop.W ord.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
Are you using the Office 2007 Primary Interop Assemblies and VSTO 2005 Second Edition?
Pete
ASKER
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
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
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
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
ASKER
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