如何按位置顺序将所有书签元素的列表从 Word 文档获取到数组:VBA / Word

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14725395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:29:37  来源:igfitidea点击:

How to get list of all bookmark-elements from a Word document to an array in order by location: VBA / Word

vbams-wordword-vba

提问by Mana

I want to fetch all the bookmarks in my Word document, and then push them to an array. The bookmarks must be sorted by their location in document not by name.

我想获取 Word 文档中的所有书签,然后将它们推送到一个数组中。书签必须按它们在文档中的位置而不是按名称排序。

ex. here's a list of bookmarks in a document,

前任。这是文档中的书签列表,

[bm_s]  (header)
[bm_h]  (title)
[bm_a]  (footer)

I want the bookmarks to keep their order so that the array will look like as following,

我希望书签保持其顺序,以便数组如下所示,

array {bm_s, bm_h, bm_a, }

ex. how it should not look like below,

前任。它不应该像下面这样,

array {bm_a, bm_h, bm_s, }

I got the fetching of all the bookmarks from document working. I get all the bookmarks in random order when fetching and pushing to the array.

我从文档工作中获取了所有书签。在获取和推送到数组时,我以随机顺序获取所有书签。

回答by Mana

Oki, so i figured it out,

好的,所以我想通了,

Here's how its done if someone else is interested in fetching all of the bookmarks with respect to its location on document.

如果其他人有兴趣获取与其在文档上的位置相关的所有书签,那么这是如何完成的。

Dim objDoc As Document
Set objDoc = ActiveDocument

For i = 1 To objDoc.Bookmarks.Count
Debug.Print objDoc.Range.Bookmarks(i) 'here you can change the code to push the bookmarks in an array
Next i

回答by SlowLearner

In case anyone still wants to know; you can access different parts of a MS Word document, like this:

万一有人还想知道;您可以访问 MS Word 文档的不同部分,如下所示:

ActiveDocument.StoryRanges(wdPrimaryHeaderStory).Bookmarks.Count
ActiveDocument.StoryRanges(wdMainTextStory).Bookmarks.Count
ActiveDocument.StoryRanges(wdPrimaryFooterStory).Bookmarks.Count