none
Word - get the element at which the cursor is RRS feed

  • Question

  • Hi All,

    I would like to know if it is possible to get the "Active Element" (for lack of a better word), that is the element at which the cursor is. So imagine the document has a table (3x3), and the user is currently in row 1,cell 2 , is it possible to somehow get the Microsoft.Office.Interop.Word.Cell? and then from there, get the Word.Row ,Word.Column, Word.Table and interact with these objects?

    Many thanks 

    Friday, November 18, 2011 9:53 AM

Answers

  • Hi mocasu

    Usually, yes, Word lets you do this. It's not possible for all objects, but for many of them:

    Word.Range rng = wdApp.Selection.Range;
    Word.Cell cel = rng.Cells[1];
    Word.Row rw = cel.Rows[1];
    Word.Table tbl = rw.Tables[1];

    Note that you can also do this:

    Word.Table tbl = wdApp.Selection.Range.Tables[1];


    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by JosephFox Saturday, November 19, 2011 3:46 PM
    • Unproposed as answer by mocasu Monday, November 21, 2011 6:50 AM
    • Marked as answer by 许阳(无锡)Moderator Tuesday, November 22, 2011 7:45 AM
    Friday, November 18, 2011 7:07 PM
    Moderator

All replies

  • Hi mocasu

    Usually, yes, Word lets you do this. It's not possible for all objects, but for many of them:

    Word.Range rng = wdApp.Selection.Range;
    Word.Cell cel = rng.Cells[1];
    Word.Row rw = cel.Rows[1];
    Word.Table tbl = rw.Tables[1];

    Note that you can also do this:

    Word.Table tbl = wdApp.Selection.Range.Tables[1];


    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by JosephFox Saturday, November 19, 2011 3:46 PM
    • Unproposed as answer by mocasu Monday, November 21, 2011 6:50 AM
    • Marked as answer by 许阳(无锡)Moderator Tuesday, November 22, 2011 7:45 AM
    Friday, November 18, 2011 7:07 PM
    Moderator
  • Hi Cindy,

    Thanks Cindy, I have been playing with some of these objects since I post this. The thing I still have not find out is to how know how to get the information of the object where the cursor is, thus to continue with the example, a 3x3 table, cursor is in row 1, cell 2:

    Word.Range rng - wdApp.Selection.Range;

    But now...how do I extract the information from that range object? How can I tell that the cursor is position in row 1, cell 2? 

    I will explain here a bit more:

    So I have a table, with ID "MediaID", and when the user is in that table, and press a hotkey, i want to insert in that row, some information: an image in cell[0] and a time in cell[1]

    This is some code that i wrote to insert the image and the time to a hard coded row, which is no used

     foreach (Microsoft.Office.Interop.Word.Table table in document.Tables)
                {
                    if (table.ID != null)
                    {
                        if (table.ID.ToString() == "MediaID")
                        {
                            try
                            {
                                int rows = table.Rows.Count;
                                int columns = table.Columns.Count;
                                Microsoft.Office.Interop.Word.Range range = table.Cell(0, 1).Range;
                                range.Delete(paramMissing, paramMissing);
                                range.InlineShapes.AddPicture(@"D:\Images\Test\LockApp.png", ref paramMissing, ref paramMissing, ref paramMissing);
                                Microsoft.Office.Interop.Word.Range range2 = table.Cell(0, 2).Range;
                                range2.Text = DateTime.Now.Hour.ToString("00") + ":" + DateTime.Now.Minute.ToString("00") + ":" + DateTime.Now.Second.ToString("00");
                                Microsoft.Office.Interop.Word.Range range3 = table.Cell(0, 3).Range;
                                range3.Select();
    
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("Error inserting text and image. Reason: " + ex.ToString());
                            }
    
    
                        }
                    }
                }
    

    One last question, I am trying to place the cursor in cell[3] by : 

                                Microsoft.Office.Interop.Word.Range range3 = table.Cell(0, 3).Range;

                                range3.Select();

    but it doesn't seem to be the right way


    Thanks 

    Monday, November 21, 2011 6:46 AM
  • Hi mocasu,

     

    Thanks for posting in the MSDN Forum.

     

    It seems you have incorrect index in your snippet. As far as I know, most of Office collection’s index begin with “1” in your snippet Cell(0,1), Cell(0,2) and Cell(0,3) will out of range. Please try Cell(1,1), Cell(1,2) and Cell(1,3) in your snippet. And I can’t find other error in your snippet.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, November 22, 2011 7:45 AM
    Moderator
  • Thanks Tom!

    Thanks for pointing that out, i was experiencing some odd behaviour !


    So, based on cindy sample, the "rng.Cells[1]" is not going retrieving cell with index 1, it is just retrieving the cell where the cursor is!...This is what got me confused. I have another question but will open a new post

    Anyway, based on Cindy's sample:

    Word.Range rng = wdApp.Selection.Range;
    Word.Cell cel = rng.Cells[1];
    Word.Row rw = cel.Rows[1];
    Word.Table tbl = rw.Tables[1];

    I get the following errors trying to access the row, and the table

    1) 'Microsoft.Office.Interop.Word.Cell' does not contain a definition for 'Rows' and no extension method 'Rows' accepting a first argument of type 'Microsoft.Office.Interop.Word.Cell' could be found (are you missing a using directive or an assembly reference?) 

    2)'Microsoft.Office.Interop.Word.Row' does not contain a definition for 'Tables' and no extension method 'Tables' accepting a first argument of type 'Microsoft.Office.Interop.Word.Row' could be found (are you missing a using directive or an assembly reference?) D:\Development\2011\WReQuest\WordControl\WinWordControl.cs 747 58 WordControl

    My references are to:

    Microsoft.Office.Core : Microsoft Office 14.0 Object Library
    Microsoft.Office.Interop.Word: 12.0.0.0

    Updated sample code as follows:

    Word.Range rng = wdApp.Selection.Range;
    Word.Cell cel = rng.Cells[1];
    Word.Row rw = cel.Rows[1];  -->  Microsoft.Office.Interop.Word.Row rw = cel.Row;
    Word.Table tbl = rw.Tables[1];  --> Microsoft.Office.Interop.Word.Table tbl = rw.Parent as Microsoft.Office.Interop.Word.Table;

    And seems to work fine!

    Thanks all



    Tuesday, November 22, 2011 10:07 AM
  • Hi mocasu

    Two things that could help you in the future...

    <<Microsoft.Office.Core : Microsoft Office 14.0 Object Library
    Microsoft.Office.Interop.Word: 12.0.0.0>>

    This shouldn't work correctly, perhaps there's a typo in it... The values of the Office libaray PIAs need to match. Either everything 14.0 or 12.0, but not mixed. If your solution really contains these, then you should correct this as it can definitely cause problems and they will be very difficult to track down.

    << get the following errors trying to access the row, and the table>>

    I usually place a using statement at the top of my code modules, such as:

    using Word = Microsoft.Office.Interop.Word;

    That allows me to use the alias Word in my code, instead of needing to type the full qualification. You can use any alias you wish, such as wd or Wd or whatever you like. It's also possible to not declare any alias, in which case the statements would be something like Cell cel = rng.Cells[1];


    Cindy Meister, VSTO/Word MVP
    Friday, November 25, 2011 10:42 AM
    Moderator