views:

88

answers:

2

Hi, I am working With Microsoft Word VBA,macros,.net My question : Is there way to get sub-topic and master-topic that precedes a selected body text ?

Here is an example:

  • Master topic (level 1)
  • sub-topic 1 (level 2)
    • body text a
    • body text b body text c
  • sub-topic 2 (level 2)
    • body text d
    • body text e
  • Other MISC topics (level 2) body text f body text g body text h

Here if bodytext e is selected I would like to run a macro that gives a result text
Master topic:sub-topic 1. I have tried with range,parent ,Scope.Information(wdActiveEndSectionNumber) etc but nothing seem to work!!!

Thanks in Advance

A: 

paragraph levels are part of an enum. All you have to do is keep track of what paragraph level you are at and then grab sub level items.

    static void Main(string[] args)
    {
        Application wrd = new Application();
        Document d;
        Documents docs = wrd.Documents;
        object readOnly = true;
        object fileName = @"C:\Users\v-chrha\Desktop\text doc.docx";
        object missing = Missing.Value;
        d = docs.Open(ref fileName, ref missing, ref readOnly, 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);

        int previousLevel = 0;
        int currentLevel = 0;
        foreach (Paragraph p in d.Paragraphs)
        {
            Console.WriteLine("Paragraph: {0}\nLevel: {1}", p.Range.Text, p.p.OutlineLevel.ToString());
            switch (p.OutlineLevel)
            {
                case WdOutlineLevel.wdOutlineLevel1:
                    currentLevel = 1;
                    break;
                case WdOutlineLevel.wdOutlineLevel2:
                    currentLevel = 2;
                    break;
                case WdOutlineLevel.wdOutlineLevel3:
                    currentLevel = 3;
                    break;
                case WdOutlineLevel.wdOutlineLevel4:
                    currentLevel = 4;
                    break;
                case WdOutlineLevel.wdOutlineLevel5:
                    currentLevel = 5;
                    break;
                case WdOutlineLevel.wdOutlineLevel6:
                    currentLevel = 6;
                    break;
                case WdOutlineLevel.wdOutlineLevel7:
                    currentLevel = 7;
                    break;
                case WdOutlineLevel.wdOutlineLevel8:
                    currentLevel = 8;
                    break;
                case WdOutlineLevel.wdOutlineLevel9:
                    currentLevel = 9;
                    break;
                case WdOutlineLevel.wdOutlineLevelBodyText:
                    currentLevel = 10;
                    break;
            }
            if (currentLevel > previousLevel)
                Console.WriteLine("with previous");
            else
                Console.WriteLine("not with previous");
            previousLevel = currentLevel;
        }
        Console.ReadLine();
        docs = null;
        d.Close(ref missing, ref missing, ref missing);
        d = null;
        wrd.Quit(ref missing, ref missing, ref missing);
        wrd = null;


    }
}
Chris
I'd even venutre to say that the enum value could be converted to an integer just the same. see: http://msdn.microsoft.com/en-us/library/bb237890.aspx
Chris
A: 
> 'By Dhiraj Bajracharya '2010 Sub
> getHeaddingsRecursive(arange As Range)
> If MainHeading <> "" Then Exit Sub On
> Error GoTo err
>     If Subheading = "" Then
>         If arange.Paragraphs(1).OutlineLevel =
> WdOutlineLevel.wdOutlineLevel2 Then
>             Subheading = arange.Text
>             Exit Sub
>         End If
>     End If
>     If arange.Paragraphs(1).OutlineLevel =
> WdOutlineLevel.wdOutlineLevel1 Then
>          MainHeading = arange.Text
>     End If    Call getHeaddingsRecursive(arange.Previous(wdParagraph,
> 1)) err: End Sub

This recursive function works and the output is stored in heading and subheading.

Thunder