none
How to select all tables in a word document using vsto c# RRS feed

  • Question

  • Hi All,

    Can anyone suggest a better way to select all tables (manualy ctrl + select table )  from the active document.

    I am using word 2013 and vsto with c#.

    Thanks


    Antony


    Wednesday, April 19, 2017 12:58 PM

Answers

  • Hello Antony,

    I agree with Paul. We need to iterate through the tables.

    What code do you use now? You could test the following code.

                Word.Application app = Globals.ThisAddIn.Application;            
                foreach (Word.Table tbl in app.ActiveDocument.Tables) {
                    tbl.Range.Editors.Add(WdEditorType.wdEditorEveryone);
                }
                app.ActiveDocument.SelectAllEditableRanges(WdEditorType.wdEditorEveryone);
                app.ActiveDocument.DeleteAllEditableRanges(WdEditorType.wdEditorEveryone); 
    

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 20, 2017 8:15 AM
    Moderator

All replies

  • You can't do that programmatically; you need to iterate through the tables.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, April 19, 2017 8:22 PM
  • But how kutools for word achived this functionality. And also i found some vba code from this link works. 

    I converted the same code in vsto and its not worked.


    Antony

    Thursday, April 20, 2017 4:17 AM
  • If you look at the code in the link you posted, you'll see it uses a loop to iterate through the tables collection, adding them to Editors so they can subsequently be selected. You could implement the same approach - using the same objects - in C#. Presumably KuTools does something similar.

    That said, the technique would be problematic in a document with other ranges already assigned the wdEditorEveryone property.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Thursday, April 20, 2017 7:01 AM
    Thursday, April 20, 2017 6:53 AM
  • Hello Antony,

    I agree with Paul. We need to iterate through the tables.

    What code do you use now? You could test the following code.

                Word.Application app = Globals.ThisAddIn.Application;            
                foreach (Word.Table tbl in app.ActiveDocument.Tables) {
                    tbl.Range.Editors.Add(WdEditorType.wdEditorEveryone);
                }
                app.ActiveDocument.SelectAllEditableRanges(WdEditorType.wdEditorEveryone);
                app.ActiveDocument.DeleteAllEditableRanges(WdEditorType.wdEditorEveryone); 
    

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 20, 2017 8:15 AM
    Moderator
  • Yes. its working Celeste. Thanks

    Antony

    Thursday, April 20, 2017 8:35 AM