none
Table Formating using c# RRS feed

  • Question

  • I tried this: (Of course, after setting  the Application & Doc Objects)

    Word.Style style = doc.Styles.Add("TableStyle", Word.WdStyleType.wdStyleTypeTable);
            style.Table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
            style.Table.Borders.OutsideLineWidth = Word.WdLineWidth.wdLineWidth150pt;
            style.Table.Borders[Word.WdBorderType.wdBorderVertical].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
            style.Table.Borders[Word.WdBorderType.wdBorderVertical].LineWidth = Word.WdLineWidth.wdLineWidth075pt;

    I received an error: Unable to perform the operation

    in VBA It works properly.

     What is wrong? 
    Thanks in advance!
    Wednesday, February 5, 2014 3:14 PM

Answers

  • Hello elischein,

    Did you try to debug the code? What line of code fires the exception?

    Also if you develop a standalone application or VSTO based add-in, I'd recommend releasing underlying COM objects instantly, for example:

    Word.Style style = doc.Styles.Add("TableStyle", Word.WdStyleType.wdStyleTypeTable);

    The Styles property of the Document class returns an instance of the Styles class which should be released after. Moreover, the Add method of the Styles class returns a new instance of the Style class which is not released too.

    style.Table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
    The Table property of the Style class returns an instance of the TableStyle class which should be released after. Each time you call the Table property the reference counter is increased. Then you call the Borders property which returns an instance of the Borders class. It is not released too, but the reference counter is increased.

    Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Word object when you have finished using it. Set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about this in the Systematically Releasing Objects article in MSDN. Though it is related to Outlook, the same can be applied to Word.


    • Marked as answer by elischein Thursday, February 6, 2014 5:08 PM
    • Edited by Eugene Astafiev Thursday, February 6, 2014 5:17 PM typos
    Wednesday, February 5, 2014 3:35 PM

All replies

  • Hello elischein,

    Did you try to debug the code? What line of code fires the exception?

    Also if you develop a standalone application or VSTO based add-in, I'd recommend releasing underlying COM objects instantly, for example:

    Word.Style style = doc.Styles.Add("TableStyle", Word.WdStyleType.wdStyleTypeTable);

    The Styles property of the Document class returns an instance of the Styles class which should be released after. Moreover, the Add method of the Styles class returns a new instance of the Style class which is not released too.

    style.Table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
    The Table property of the Style class returns an instance of the TableStyle class which should be released after. Each time you call the Table property the reference counter is increased. Then you call the Borders property which returns an instance of the Borders class. It is not released too, but the reference counter is increased.

    Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Word object when you have finished using it. Set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about this in the Systematically Releasing Objects article in MSDN. Though it is related to Outlook, the same can be applied to Word.


    • Marked as answer by elischein Thursday, February 6, 2014 5:08 PM
    • Edited by Eugene Astafiev Thursday, February 6, 2014 5:17 PM typos
    Wednesday, February 5, 2014 3:35 PM
  • As Eugene mentions, WHICH line of code is triggering the error?

    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, February 6, 2014 4:35 PM
    Moderator
  • The second line,

    I fixed it:

                Word.Style style = doc.Styles.Add("myStyle", Word.WdStyleType.wdStyleTypeTable);
                Word.TableStyle ts = style.Table;
                Word.Borders tsb = ts.Borders;
                tsb.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                tsb.OutsideLineWidth = Word.WdLineWidth.wdLineWidth150pt;
                tsb[Word.WdBorderType.wdBorderVertical].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                tsb[Word.WdBorderType.wdBorderVertical].LineWidth = Word.WdLineWidth.wdLineWidth075pt;

    Now it works. 

    Anyway, thank you very much


    • Edited by elischein Thursday, February 6, 2014 5:15 PM
    Thursday, February 6, 2014 5:14 PM