Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Dear All,

I have created MsOffice Template application in windows using office interop in C#.

I want to retrieve text from table_of_contents and indexes which are already in word document.

MyCode:

What I have tried:

C#
public static bool EvaluateTableOfContents(IQuestion question, string filename)
        {
            WordInterop.Application wordApplication = GetOrCreateWordApplication(question.ObjectStore);

            try
            {
                //Avoid screen flickering or unwanted alerts while initializing
                wordApplication.ScreenUpdating = false;
                WordInterop.WdAlertLevel displayAlertLevel = wordApplication.DisplayAlerts;
                wordApplication.DisplayAlerts = WordInterop.WdAlertLevel.wdAlertsNone;

                WordInterop.Document wordDocument = wordApplication.Documents.Open(filename);

                bool result = false;

                WordInterop.TablesOfContents TOC = wordDocument.TablesOfContents;  
                
                TOC.              

                result = TOC.Count > 0;//want to retrieve text as well.

                wordDocument.Close();
                return result;

            }
            catch (Exception)
            {
                Cleanup(question.ObjectStore, true);
            }
            return false;
        }


Index
public static bool EvaluateIndexes(IQuestion question, string filename)
        {
            WordInterop.Application wordApplication = GetOrCreateWordApplication(question.ObjectStore);

            try
            {
                //Avoid screen flickering or unwanted alerts while initializing
                wordApplication.ScreenUpdating = false;
                WordInterop.WdAlertLevel displayAlertLevel = wordApplication.DisplayAlerts;
                wordApplication.DisplayAlerts = WordInterop.WdAlertLevel.wdAlertsNone;

                WordInterop.Document wordDocument = wordApplication.Documents.Open(filename);

                bool result = false;

                WordInterop.Indexes ind = wordDocument.Indexes;
                
                result = ind.Count > 0;//how to retrieve text?

                wordDocument.Close();
                return result;

            }
            catch (Exception)
            {
                Cleanup(question.ObjectStore, true);
            }
            return false;
        }


can anyone please help me.

Thanks
Posted
Updated 22-May-17 3:34am

1 solution

I got the solution

if(tableOfContents.Count > 0)
                {
                    WordInterop.Range range = tableOfContents[_index].Range;
                    if (range.Find.Execute(text))
                        result = true;
                }


if (index.Count > 0)
{
    WordInterop.Range range = index[_index].Range;
    if (range.Find.Execute(text))
        result = true;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900