Reference http://www.microsoft.com/china/msdn/library/office/office/UndstaExcelObjModNETDev.mspx
I have been registering for a while and have never had time to write something. I am so sorry.
Recently, my wife needs to count the words in a large number of Word documents.
The number of words in multiple Word documents is small. Program .
The number of words (or characters) used to read a word file. You must first reference the word object and Excel object.
// Open multiple Word files and obtain the number of words
Word. applicationclass thisapplication = new word. applicationclass ();
Object missingvalue = type. missing;
Object Mytrue = true;
Int count;
Object filename;
Foreach (listviewitem item in listview1.items) // list of multiple Word documents
{
Count = 0;
Filename = item. subitems [0]. text;
Word. Document thisdocument = thisapplication. Documents. Open (ref filename, ref missingvalue,
Ref Mytrue, ref missingvalue,
Ref missingvalue, ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue, ref missingvalue,
Ref missingvalue );
Word. Range RNG = thisdocument. content;
RNG. Select ();
Count = thisdocument. characters. count;
}
Thisdocument. Close (ref missingvalue, ref missingvalue, ref missingvalue );
// Save to an Excel file
Void savetoexcel ()
{
System. Windows. Forms. savefiledialog file = new savefiledialog ();
File. defaultext = ". Doc ";
File. Filter = "excle file (*. xls) | *. xls | all files (*. *) | *.*";
File. Title = "select the location to save the file ";
If (file. showdialog () = system. Windows. Forms. dialogresult. OK)
{
Excel. applicationclass thisapplication = new excel. applicationclass ();
Excel. Workbook workbook = thisapplication. workbooks. Add (type. Missing );
Excel. worksheet newworksheet;
Newworksheet =
(Excel. worksheet) Workbook. worksheets. Add (
Type. Missing, type. Missing );
Int I = 1;
Foreach (listviewitem item in listview1.items)
{
Excel. Range = (Excel. Range) newworksheet. cells [I, 1];
Range. value2 = item. subitems [0]. text;
Range = (Excel. Range) newworksheet. cells [I, 2];
Range. value2 = item. subitems [1]. text;
I ++;
}
Foreach (Excel. Workbook book in thisapplication. workbooks)
{
Book. savecopyas (file. filename );
Book. Close (false, type. Missing, type. Missing );
}
}
}