2011-05-09 118 views
3

我在查找单词中的特定部分时遇到问题。建议我尝试通过Word中的VB对象浏览器寻求帮助。我知道至少有5个标题“集合”(如果你在文档结构图中看到I.E.,我会看到编号为1,2,3,4,5 ...)。我不知道如何导航到第五个标题,最初我认为它是章节,但是当我查看章节时,我意识到几乎所有章节都在一个章节中,但是如果有人正在查找有关如何执行章节的信息,以下似乎工作,因为我已经经历了写它的麻烦。获取Word文档的特定部分中的段落

my($document) = $Word->Documents->Open($input) || die("Unable to open document ", Win32::OLE->LastError()); 
my $section = $document->{Sections}->Item(1); # put section number you're looking for in here 
$section_five_paragraphs = $section->{Range}->Paragraphs(); 
$enumerate = new Win32::OLE::Enum($section_five_paragraphs); 
    while (defined($paragraph = $enumerate->Next())) 
    { 
    print $paragraph->{Range}->{Text} . "\n"; 
    } 

所以没有人知道如何到达第五个标题区域,或者可以指向我可能有所帮助?

+0

我不会回答这个问题,但我只是最终通过段落做了搜索,直到我发现5. <标题文本>幸好大多数人似乎是在多个文件相同的标题,但我仍然会而是能够跳到第5个“标题1”,如果有人能回答我会很感激。 – onaclov2000 2011-05-11 12:00:26

回答

2

告诉我,如果我没有正确地关注你,但你正试图在某个部分找到第5个标题1?如果是这样的话,虽然Word清楚地定义了部分(您注意到的是$ document - > {Sections} - > Item(1)),但它并没有明确定义具体或样式中的标题。为此,你必须经历所有寻找感兴趣的风格。下面的VBA代码(和我不写perl道歉)只是这样做,只看在一个特定的部分。

Sub FindHeading1() 
    On Error GoTo MyErrorHandler 

    Dim currentDocument As Document 
    Set currentDocument = ActiveDocument 

    Dim findRange As Range 
    Set findRange = currentDocument.Sections(2).Range 'which section you want 

    Dim endRange As Long 
    endRange = findRange.end 

    findRange.Find.ClearFormatting 
    findRange.Find.Style = ActiveDocument.Styles("Heading 1") 

    Dim headingCountFound As Long 
    Do While findRange.Find.Execute(FindText:="") 
     If findRange.End > endRange Then Exit Sub 
     findRange.Select 
     headingCountFound = headingCountFound + 1 
     If headingCountFound = 3 Then 'which occurance you want 
      MsgBox "Found." 
      Exit Do 
     End If 

     DoEvents 
    Loop 

    Exit Sub 

MyErrorHandler: 
    MsgBox "FindHeading1" & vbCrLf & vbCrLf & "Err = " & Err.Number & vbCrLf & "Description: " & Err.Description 
End Sub 
+0

这看起来像可能会工作,我可以把它翻译成perl,当我有一些时间再看一遍,我会回来,让你知道如果你解决了我的问题! – onaclov2000 2011-06-06 16:43:54

相关问题