Svyatoslav Razmyslov
Svyatoslav Razmyslov

Reputation: 411

How to find the line number using Word API?

I want to know the line number in the Word document from Word.Paragraph or Word.Range, but there are no suitable fields and methods.I'm using C#.

Upvotes: 2

Views: 832

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25663

Getting the line number involves using an old part of the Word object model that comes from the Word Basic days: the Information property. Since C# doesn't "like" properties with arguments, it's the get_Information method for C#

int lineNumberSelection = WordApp.Selection.get_Information(Word.WdInformation.wdFirstCharacterLineNumber);
int lineNumberRange = myParagraph.Range.get_Information(Word.WdInformation.wdFirstCharacterLineNumber);

This returns the numbering as set in the document - it's "What you see is what you get". So if the numbering is set to restart on each page or for each section or each page - that's the result returned.

If a different result is required, the numbering rule for the document must be changed (and can be changed back again, after). For example:

wdDocument.PageSetup.LineNumbering.RestartMode = Word.WdNumberingRule.wdRestartContinuous;

Upvotes: 2

Related Questions