VSTO WORD delete selected table
-
Wednesday, February 22, 2006 4:23 AM
Hi,
I am very new to VSTO but good at .NET 2005. I would like to know - in VSTO for MS Word, Can we delete currently selected Table. Basically when I use following code lines..
Word.
Selection objSelection = Globals.ThisDocument.Application.Selection; if (objSelection.Tables.Count > 0) { Globals.ThisDocument.Tables[1].Rows.Delete(); }it delete always 1st table because tableindex given as 1. Is it possible to get index of currently selected table here?

I am also facing similar problem while try to delete current row in currently selected table as I am coding as..
Word.
Selection objSelection = Globals.ThisDocument.Application.Selection; if (objSelection.Tables.Count > 0){ Globals.ThisDocument.Tables[1].Rows[1].Delete();}
Thanks for taking interest and hope for your reply.
All Replies
-
Wednesday, February 22, 2006 12:08 PM
Hi,
Welcome to the VSTO community! Please feel free to post any questions you may have. A list of VSTO resources can be found in the following posting:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=196504&SiteID=1Many of the questions you will have are probably related to Office application specific object models. A list of Office forums can be found here: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=174275&SiteID=1
Now to your question ...
The Word.Selection.Tables collection contains only the tables that fall within the selection. You could have more than 1 tables in the user selected range.
Office.CommandBarButton button = null;
void button_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
{
Word.Selection objSelection = Globals.ThisDocument.Application.Selection;
if (objSelection.Tables.Count > 0)
// Tables[1] is the first table within the currently selected part of the doc
objSelection.Tables[1].Delete();// Rows[1] is the first Row within the current selection
// objSelection.Rows[1].Delete();// To delete all Tables in the current selection
// objSelection.Rows.Delete();
}// Just adds a button to Word commandbar that calls button_Click method
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
button = (Office.CommandBarButton)this.CommandBars["Standard"].Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true);
button.Caption = "Del Table";
button.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
button.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(button_Click);
}Hope this helps.
-
Thursday, February 23, 2006 10:32 AM
Hi Apurva,
Thanks a lot for prompt and perfect reply and so your reply really work for both cases.

-
Thursday, February 23, 2006 10:54 AM
Hi,
I am also wondering to delete selected row/rows using VSTO Excel. I code it following way..
public void DeleteRow()
{
Excel.Worksheet ObjThisWorksheet = (Excel.Worksheet)this.InnerObject;
Excel.Range ObjCurRange = (Excel.Range)ObjThisWorksheet.Cells[3, 4];
Excel.Range ObjCurRow = ObjCurRange.EntireRow;
ObjCurRow.Delete(Excel.XlInsertShiftDirection.xlShiftDown);}
This could delete 2nd Row as in cell 3,4 is hard coded. but how can I make it generalise so that it can delete any currently selected row. How can I get current selected row / cell stuff.
In word we have Word.Selection object but in Excel how can I.

Thank you for your interest and hope for reply.
-
Thursday, February 23, 2006 10:54 AMSorry it deletes 3rd Row..as in cell 3,4.
-
Friday, February 24, 2006 6:13 AM
Hi,
Excel Application also has a Selection object that can be used to delete any row that is is included in the current selectionOffice.
CommandBarButton cbButton; private void Sheet1_Startup(object sender, System.EventArgs e)
{
// Add a button to the Excel Toolbar to call our deletion method
cbButton = (Office.CommandBarButton)this.Application.CommandBars["Standard"].Controls.Add(
Office.MsoControlType.msoControlButton, missing, missing, missing, true);
cbButton.Caption = "Test";
cbButton.Style = Office.MsoButtonStyle.msoButtonCaption;
cbButton.Visible = true;
cbButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(cbButton_Click);
} void cbButton_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
{
Excel.Range selection = (Excel.Range)this.Application.Selection;
int rowsToDelete = selection.Rows.Count; for (int i = 1; i <= rowsToDelete; i++)
{
// Keep deleting first row in the selection rowsToDelete times because as we
// delete rows they shift up in the selection area, like a stack
((Excel.Range)selection.Rows[1, missing]).EntireRow.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);
}
} -
Monday, June 05, 2006 3:41 PM
Thanks to All. It works.

