VSTO gets the selection of Office documents (Word, Excel, PPT, Outlook)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.