获取Word文档特定部分中的段落
我在查找 Word 中的特定部分时遇到问题。建议我尝试通过 Word 中的 VB 对象浏览器寻求帮助。我知道至少有 5 个标题“集”(即,如果您查看文档结构图,我会看到编号为 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";
}
那么有人知道如何到达第五个标题区域,或者可以给我指出一些可能有帮助的东西吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没有正确理解您,但您正在尝试在某个部分中查找第 5 个标题 1,请告诉我?如果是这种情况,尽管 Word 明确定义了节(您将其记为 $document->{Sections}->Item(1)),但它并没有明确定义具体的标题或一般样式。为此,您必须浏览所有样式以寻找感兴趣的样式。以下 VBA 代码(很抱歉没有编写 perl)就是这样做的,并且仅在特定部分中查找。
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.