naqushab naqushab - 2 years ago 212
C# Question

Word Interop - Check if a doc/x file has Superscript/Subscript text or Equations

I am building a Word File Content Analyser and I am stuck with the code, on how to check if a doc/docx file has Superscripts/Subscripts or Equations in the content.

Code I have so far is:

WordApp = new Microsoft.Office.Interop.Word.Application();
WordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
WordApp.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;
WordApp.Visible = false;
WordApp.Options.SaveInterval = 0;
Document DocObj = WordApp.Documents.Open(FileName, OpenAndRepair: false, NoEncodingDialog: true, ConfirmConversions: false, ReadOnly: true, PasswordDocument: "dummy", PasswordTemplate: "dummy");

PageCount = DocObj.ActiveWindow.ActivePane.Pages.Count;

I know that I have to use
to select the ranges but I don't know how to do with it.

Also, the equations are those which are created using Insert/Equations panel.

Answer Source

If you just want to check if any part of the main document story range (doesn't include Headers, Footers and other WdStoryType) then something like:

var font = DocObj.Content.Font;
var hasSuperscripts = font.Superscript != 0;
var hasSubscripts = font.Subscript != 0;
var hasEquations = DocObj.OMaths.Count > 0;

Most of the Word Range properties that return int instead of bool (like .Font.Superscript and .Font.Subscript) return -1 if they are true for the whole range, 0 if they are false for the whole range, or 9999999 (wdUndefined) if they are true only for part of the range.

If you want to search the rest of the .StoryRanges too:

var hasSubscripts = DocObj.StoryRanges.Cast<Range>().Any(r => r.Font.Subscript != 0);
var hasSuperscripts = DocObj.StoryRanges.Cast<Range>().Any(r => r.Font.Superscript != 0);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download