0

I want to count the total number of lines of the word document (.doc /.docx).

In my class, I added a reference to the COM library Microsoft.Office.Interop.Word through which I am counting the document's total word count.

With reference to the this Lines.Count Property documentation, the library also provides a lines count option in the latest version.

But unfortunately, I am unable to find the Lines interface or property in the whole library. Is there any other way to get the total number of lines of the MS Word document as shown in the image below?

Click here to view image

Method for words count (just for reference)

public int GetWordsCountFromWordFile(string wordFile)
    {
        try
        {
            if (!string.IsNullOrEmpty(wordFile))
            {
                var application = new Application();
                var document = application.Documents.Open(wordFile, ReadOnly: true);
                int count = document.Words.Count;
                document.Close();
                return count;
            }
            return 0;
        }
        catch (Exception ex)
        {
            LogWriter.ErrorLogWriter(nameof(Client), nameof(TaskHelper), nameof(GetWordsCountFromWordFile), "int", ex.Message);
            return 0;
        }
    }
  • 1
    You could use the [`built in properties`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word._document.builtindocumentproperties?view=word-pia), and get the `wdPropertyLines` to get Lines count. – Anand Sowmithiran Jun 15 '22 at 12:54
  • Agree with @AnandSowmithiran. BTW the lines property you referenced is accessible through this path `document->windows->panes->pages->rectangles->lines` – Ilia Maskov Jun 15 '22 at 13:02
  • @IliaMaskov I check but was unable to get panes after `document->windows`. Note: I assume that the `document` referred to here is the same document variable in my code in the question I asked. – Tiny Developer Jun 15 '22 at 13:27

1 Answers1

0

Answering my own question because I found the easiest and shortest working solution to the problem, exactly like I wanted it.

I added up two more lines of code in the method provided in my question and I got accurate results.

int lines = document.ComputeStatistics(WdStatistic.wdStatisticLines, true);
application.Quit(WdSaveOptions.wdDoNotSaveChanges);

Note:

Sending true parameter in ComputeStatistics method to include footnotes and endnotes.

Quit will stop the save changes window from opening and will close the MS Word process running in the background.