.NET实现ms-Word中字数统计

今天,研究了一下C#对Word字数的统计:

    先添加引用;

    引入命名空间using Word = Microsoft.Office.Interop.Word;

   wordDoc为Word Document的对象

    Microsoft.Office.Interop.Word.Range docRanger = wordDoc.Content;
        //页数
        Pages = docRanger.ComputeStatistics(Word.WdStatistic.wdStatisticPages);
        //字数
        Words = docRanger.ComputeStatistics(Word.WdStatistic.wdStatisticWords);
        //字符数(不计空格)
        Characters = docRanger.ComputeStatistics(Word.WdStatistic.wdStatisticCharacters);
        //字符数(计空格)
        Characterswithspaces = docRanger.ComputeStatistics(Word.WdStatistic.wdStatisticCharactersWithSpaces);
        //段落数
        Paragraphs = docRanger.ComputeStatistics(Word.WdStatistic.wdStatisticParagraphs);
        //行数
        Lines = docRanger.ComputeStatistics(Word.WdStatistic.wdStatisticLines);
        //中文字符和朝鲜语单词
        Fareastcharacters = docRanger.ComputeStatistics(Word.WdStatistic.wdStatisticFarEastCharacters);
        //非中文单词
        Notchinese = Words - Fareastcharacters;
        //插图数
        int count_picture = 0;
        foreach (Word.InlineShape ish in wordDoc.InlineShapes)
        {
            if ((ish.Type == Word.WdInlineShapeType.wdInlineShapeLinkedPicture) || (ish.Type == Word.WdInlineShapeType.wdInlineShapePicture))
            {
                count_picture++;
            }
        }
        Photos = count_picture;

你可能感兴趣的:(C#)