Original: VSTO Gets the selection of Office documents (Word, Excel, PPT, Outlook)
Purpose: Get a piece of content selected in Word, Excel, PPT, Outlook.
Word:
private String Getselectcont ()
{
string w = "";
word.selection sec = appword.selection;
Word.words WDS = sec. Words;
W = WDS. Application.Selection.Text;
return W.trim ();
}
Outlook:
private String Getselectcont (Outlook.inspector Inspector)
{
string w = "";
word.document Document = inspector.wordeditor;
W = document. Application.Selection.Words.Application.Selection.Text;
return W.trim ();
}
Note: You cannot get a selection such as a title.
Ppt:
private String Getselectcont ()
{
string w = "";
powerpoint.selection sec = appPPT.ActiveWindow.Selection;
String Word = sec. TextRange.Text;
return word. Trim ();
}
Excel:
private String Getselectcont ()
{
string w = "";
object[,] result;
Object res1;
String Res2;
Excel.Workbook Wbook = Globals.ThisAddIn.Application.ActiveWorkbook; Current Activity Workbook
Excel.Worksheet Wsheet = (excel.worksheet) wbook. ActiveSheet; Current Activity Sheet
Excel.Range Range = (excel.range) wsheet. Application.selection; The currently selected cells
int count = range. Count;
Res1 = (object) range. Value2;
If multiple cells are selected
if (Count > 1)
{
int row_count = range. Rows.Count;
int col_count = range. Columns.count;
result = (object[,]) res1;
for (int i = 1; I <= row_count; i++)
{
for (int j = 1; J <= Col_count; j + +)
{
if (Result[i, j]! = NULL)
{
W + = (string) result[i, J] + "";
}
}
}
}
else {
If you select a single
if (res1 = = null)
{
W = "";
}
else {
Res2 = Res1. ToString ();
W = res2;
}
}
return W.trim ();
}
Note: Excel is more complex, which only gets the contents of single or multiple cells selected, but does not get the contents of the mark in a cell, looking for a solution ...
VSTO gets the selection of Office documents (Word, Excel, PPT, Outlook)