Search code examples
c#winformsms-wordoffice-interop

Get Number of Characters with Space from Word Document


I am using Microsoft.Office.Interop.Word to count number of characters with space but when i am using inbuilt function i.e count it gives me more count then viewing manually from word document.

 Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
 doc = word.Documents.Open(ref fileName,
                ref missing,ref missing,ref missing,ref missing,
                ref missing,ref missing,ref missing,ref missing,
                ref missing,ref missing,ref missing,ref missing,
                ref missing,ref missing,ref missing);                
            doc.Activate();
            double count = doc.Content.Characters.Count;

so, is this any method which can directly give characters with space?


Solution

  • try this

    Microsoft.Office.Interop.Word.Range rng = doc.Content; 
    rng.Select(); 
    int nb = rng.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticCharactersWithSpaces);