2010-05-12 81 views
1

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

下面是一个例子:

  • 万事达主题(1电平)
  • 子主题1(级别2)
    • 正文一个
    • 体化文本B正文c
  • 子主题2(级别2)
    • 正文d
    • 体文本电子邮件
  • 其他MISC主题(2级)正文F主体文本克正文ħ

这里如果bodyText的Ë选择,我想运行宏,给出了一个结果文本
主题:sub-topic 1。 我尝试过range,parent ,Scope.Information(wdActiveEndSectionNumber)等,但似乎没有任何工作!

回答

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 

这个递归函数的工作原理和输出存储在标题和副标题。

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; 


    } 
} 
+0

我什至venutre说,枚举值可以转换为一个整数相同。请参阅:http://msdn.microsoft.com/en-us/library/bb237890.aspx – 2010-05-12 20:55:00