获取Word文档特定部分中的段落(Get Paragraphs in specific Section of Word Document)

编程入门 行业动态 更新时间:2024-10-26 12:21:50
获取Word文档特定部分中的段落(Get Paragraphs in specific Section of Word Document)

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

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"; }

那么有谁知道如何到达这个第5个标题区域,或者可以指出一些可能有用的东西?

I'm having problems finding a specific section in word. It was recommended I try looking through the VB Object Browser in Word for help. I know there are at least 5 heading "sets" (I.E. if you look in the Document Map, I see numbered 1,2,3,4,5...). I don't know how to navigate to that fifth heading, initially I thought it was sections, but when I viewed sections I realized that almost all of it is in one section, but in case anyone is looking for information on how to do sections, the below seems to work, since I already went through the trouble of writing it.

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"; }

So does anyone know how to get to this 5th heading area, or can point me to something that might help?

最满意答案

告诉我,如果我没有正确地关注你,但你想在某个部分找到第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

Tell me if I didn't follow you correctly but you're trying to find the 5th Heading 1 in the a certain section? If that's the case, although Word clearly defines sections (which you note as $document->{Sections}->Item(1)), it does not clearly define Headings in specific or styles in general. For that you'll have to go through all the styles looking for those of interest. The following VBA code (and I apologize for not writing perl) does just that and looks only in a specific section.

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

更多推荐

本文发布于:2023-08-06 05:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1443603.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:段落   文档   Word   Paragraphs   Document

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!