angel_neo
angel_neo

Reputation: 331

Microsoft.Office.Interop.word get table cell value

I'm using Microsoft.Office.Interop.word to create a little table in a Word Document and I'm triying to get a table cell value for some calculates (I know is not the optimal, but for our customer it is) When I get the cell value, I get the value with "\r\a" and I cannot convert it to decimal or int. In my code, this line is the problem: "string text = cell.Range.Text"

protected void test ( object sender, EventArgs e ) {

        
        try {
            Microsoft.Office.Interop.Word._Application objApp;
            Microsoft.Office.Interop.Word._Document objDoc;
            object objMiss = System.Reflection.Missing.Value;
            object objEndOfDocFlag = "\\endofdoc";*/


            //Start Word and create a new document.
            objApp = new Microsoft.Office.Interop.Word.Application();
            //objApp.Visible = true;
            objDoc = objApp.Documents.Add( ref objMiss, ref objMiss, ref objMiss, ref objMiss );


            //Insert a 2 x 2 table, (table with 2 row and 2 column)
            Microsoft.Office.Interop.Word.Table objTab1; //create table object
            Microsoft.Office.Interop.Word.Range objWordRng = objDoc.Bookmarks.get_Item( ref objEndOfDocFlag ).Range; //go to end of document
            periodos = Convert.ToInt32( txt_plazo.Text ) / 30;
            objTab1 = objDoc.Tables.Add( objWordRng, periodos, 7, ref objMiss, ref objMiss ); //add table object in word document
            objTab1.Range.ParagraphFormat.SpaceAfter = 6;


            int iRow, iCol;
            decimal deudaInicial = Convert.ToDecimal( txt_valor.Text); // Deuda Inicial
            decimal tasaInteres = Convert.ToDecimal( txt_TasaEfe.Text ); // Deuda Inicial

            for ( iRow = 1; iRow <= 1; iRow++ ) { // table headers

                objTab1.Cell( iRow, 1 ).Range.Text = "Periodo";
                objTab1.Cell( iRow, 2 ).Range.Text = "Deuda Inicial";
                objTab1.Cell( iRow, 3 ).Range.Text = "Tasa Interes";
                objTab1.Cell( iRow, 4 ).Range.Text = "Interes";
                objTab1.Cell( iRow, 5 ).Range.Text = "Amortizacion";
                objTab1.Cell( iRow, 6 ).Range.Text = "Pago";
                objTab1.Cell( iRow, 7 ).Range.Text = "Deuda Final";
            }



            for ( iRow = 2; iRow <= periodos; iRow++ ) {


                //periodo
                for ( iCol = 1; iCol <= 1; iCol++ ) {
                    objTab1.Cell( iRow, iCol ).Range.Text = iRow.ToString();
                }


                //periodo
                for ( iCol = 1; iCol <= 1; iCol++ ) {
                    objTab1.Cell( iRow, iCol ).Range.Text = iRow.ToString();
                }


                // Deuda Inicial
                for ( iCol = 2; iCol <= 2; iCol++ ) {
                    objTab1.Cell( iRow, iCol ).Range.Text = deudaInicial.ToString();
                }


                // tasa interes
                for ( iCol = 3; iCol <= 3; iCol++ ) {
                    objTab1.Cell( iRow, iCol ).Range.Text = tasaInteres.ToString();
                }


                // Interes
                for ( iCol = 4; iCol <= 4; iCol++ ) {
                    //decimal a = Convert.ToDecimal( objTab1.Cell( iRow, iCol - 2 ).Range.Text);
                    //decimal b = Convert.ToDecimal( objTab1.Cell( iRow, iCol - 1 ).Range.Text);
                    //objTab1.Cell( iRow, iCol ).Range.Text = ( a * b / periodos ).ToString();

                    var cell = objTab1.Cell( iRow, iCol-2 );
                    string text = cell.Range.Text; // *** HERE IS MY PROBLEM ***, :(

                }
            }

            objTab1.Rows[1].Range.Font.Bold = 1; //make first row of table BOLD
            //objTab1.Columns[1].Width = objApp.InchesToPoints( 3 ); //increase first column width
            objTab1.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
            objTab1.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;




            //Add some text after table
            objWordRng = objDoc.Bookmarks.get_Item( ref objEndOfDocFlag ).Range;
            objWordRng.InsertParagraphAfter(); //put enter in document
            objWordRng.InsertAfter( "THIS IS THE SIMPLE WORD DEMO : THANKS YOU." );

            object szPath = ConfigurationManager.AppSettings["rutaConvenio"].ToString() + "tabla" + ".docx"; //guardar
            objDoc.SaveAs2( ref szPath );
            objDoc.Close();
            objApp.Quit();


        } catch ( Exception ex ) {

            Console.WriteLine( "Error occurred while executing code : " + ex.Message );
        } finally {
            //you can dispose object here
        }

    }

how can I get the desired value ? I hope please somebody can help me.

best regards

Upvotes: 0

Views: 1832

Answers (1)

JohnG
JohnG

Reputation: 9469

From this previously asked question…

What is this format code (\r\a) I get when using office interop in c#?

and my small tests it does indeed appear that …

string text = cell.Range.Text

returns some extra characters. As noted in the above link, the two characters are an “end-of-cell/end-of-row" characters coming from the table structure.

Unfortunately, the only proffered solution was to simply “remove” the unwanted characters. And in my small tests this does appear to be at least a solution as I was unable to get the text from a cell without the extra “\r\a” characters.

Unfortunately… the code in the above linked accepted answer appears to be malformed and should work if fixed, however, I suggest a simpler “Regular Expression” solution which can be implemented in one line of code. Something like…

var cell = objTab1.Cell(iRow, iCol - 2);
string text = Regex.Replace(cell.Range.Text, "\r\a", "");
decimal.TryParse(text, out decimal value);
Debug.WriteLine("decimal Value: " + value);

The Regex will require the necessary using statement…

using System.Text.RegularExpressions;

I may do some more tests later to see if there is possibly a better solution, however, this solution seems fairly simple and straight-forward.

Upvotes: 0

Related Questions