none
VSTO Word Cursor position - select - parent width RRS feed

  • Question

  • Hi - I am trying to get the width of the nearest parent object to my cursor position in word

    I want to place an Image/Shape INLINE at the cursor position and scale it to the parent cell/column OR paragraph OR page width so as not to break the layout

    in C# I get the range selected

    --------------------------------------------------------------------------------------------------------------

    Word.Range selection = vstoDocument.Application.Selection.Range;

    --------------------------------------------------------------------------------------------------------------

    I did some tests to see where the cursor is - It would be wonderful if there was some tool that allowed you to navigate the DOM tree of the word document (like we have with JQuery) and see what the closest parent object is to use to measure the width of a container object

    It could be: paragraph, Column, Cell

    ----------------------------------------------------------------------------------

    float targetWidth = selection.Columns.Width;

    --------------------------------------------------------------------------------------

    Column.width works only if the table does not consist of cells that have been spanned/merged

     Debug.WriteLine("selection.Cells.NestingLevel" + selection.Cells.Count);
         Debug.WriteLine("selection.Rows.Count " + selection.Rows.Count);
         Debug.WriteLine("selection.Rows.NestingLevel " + selection.Rows.NestingLevel);
         Debug.WriteLine("selection.Columns.Count " + selection.Columns.Count);

    selection.Cells.Count - gives me the amount of Cells selected - but I cannot seem to get to something like

    theCurrentCell

    -------------------------------------------------------------------------------------------------

    selection.Cells[0].width

    --------------------------------------------------------------------------------------------------

    throws error

    because it is a "dynamic" property

    I can get to it in the Debugger

    ----------------------------------------------------------------------------------------------------

    ((dynamic)new System.Linq.SystemCore_EnumerableDebugView(selection.Cells as System.Collections.IEnumerable).Items[0]).Width

    ----------------------------------------------------------------------------------------------------

    any ideas

    Thanks


    • Edited by Jaco Graaff Tuesday, August 20, 2013 12:02 AM
    Tuesday, August 20, 2013 12:01 AM

Answers

  • I tried to use reflection

    I also tried to use

    ImpromptuInterface

    My solution - 

    If the range/selection is of type "insertion point"

    and

    selections.Columns does not throw an error - I can assume I am in a single cell

    then I run this:

    ----------------------------------------

    Microsoft.Office.Interop.Word.Cells cells = selection.Cells;
    foreach(Microsoft.Office.Interop.Word.Cell cell in cells)
    {
     Debug.WriteLine("cell width: " + cell.Width);
    }

    -----------------------------------------

    • Marked as answer by Jaco Graaff Tuesday, August 20, 2013 5:20 AM
    Tuesday, August 20, 2013 3:50 AM

All replies

  • I am trying to cast the objects to an IEnumerable collection of Cells type

    The problem i that the collection is a dynamic object and only get's determined at run-time - 

    casting does not work - I still get the error

    //System.Collections.IEnumerable<Microsoft.Office.Interop.Word.Cells> query = selection.Cells.Cast<Microsoft.Office.Interop.Word.Cells>();

    another option is to try and get the variable through get property:

    (cells[0]).GetType().GetProperty("Width").GetValue(cells[0], null));

    Still no solution...

    Tuesday, August 20, 2013 2:18 AM
  • I tried to use reflection

    I also tried to use

    ImpromptuInterface

    My solution - 

    If the range/selection is of type "insertion point"

    and

    selections.Columns does not throw an error - I can assume I am in a single cell

    then I run this:

    ----------------------------------------

    Microsoft.Office.Interop.Word.Cells cells = selection.Cells;
    foreach(Microsoft.Office.Interop.Word.Cell cell in cells)
    {
     Debug.WriteLine("cell width: " + cell.Width);
    }

    -----------------------------------------

    • Marked as answer by Jaco Graaff Tuesday, August 20, 2013 5:20 AM
    Tuesday, August 20, 2013 3:50 AM