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

老子叫甜甜 提交于 2019-12-07 13:27:17

问题


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.

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.


回答1:


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



回答2:


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

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


来源:https://stackoverflow.com/questions/14725395/how-to-get-list-of-all-bookmark-elements-from-a-word-document-to-an-array-in-ord

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!