获取单词中所选文本的标题

时间:2010-05-12 11:19:06

标签: c# vb.net vba ms-word

我正在使用Microsoft Word VBA,宏,.net 我的问题:是否有办法在选定的正文文本之前获得子主题和主题?

以下是一个例子:

  • 主题(第1级)
  • 子主题1(第2级)
    • 正文
    • 正文b正文c
  • 子主题2(第2级)
    • 正文d
    • 正文e
  • 其他MISC主题(第2级)正文文本f正文g正文h

如果选择 bodytext e ,我想运行一个提供结果文本的宏
主题:子主题1 。 我尝试了range,parent ,Scope.Information(wdActiveEndSectionNumber)等,但似乎没有任何作用!!!

2 个答案:

答案 0 :(得分:0)

段落级别是枚举的一部分。您所要做的就是跟踪您所处的段落级别,然后获取子级别项目。

    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;


    }
}

答案 1 :(得分:0)

> '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

此递归函数有效,输出存储在标题和子标题中。