Accessing a list of bookmarks by location

K

Keith

Hello, I am using a bookmarks collection in VB6 that accesses a word doc but
can't get the list of bookmarks to come back in order of location. Any ideas?

Thanks,
Keith
 
S

Shauna Kelly

Hi Keith

With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByLocation
.ShowHidden = True
End With

For what it's worth, the macro recorder is a good way to identify the
methods and properties you need. This code is what it produced when I
simply showed the Insert > Bookmark dialog and got it to sort by
location and show hidden bookmarks.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
K

Keith

I tried that but it doesn't seem to work. Here my code...

Set oWord = New Word Application
Set oDoc = oWord.Documents.add(gsTemplateLocation & sDoc)

oDoc.Bookmarks.DefaultSorting = wdSortByLocation

For i = 1 to oDoc.Bookmarks.Count
debug.Print oDoc.Bookmarks(i).Name
Next i
 
K

Keith

Nevermind, i found out the problem. I don't understand it yet but replacing
debug.Print oDoc.Bookmarks(i).Name
with
debug.Print oDoc.Range.Bookmarks(i).Name
worked.
 
J

Jay Freedman

Sorry to step in here late...

Don't bother with the DefaultSorting property, it doesn't help with this.

Instead of calling for oDoc.Bookmarks(i).Name, call for
oDoc.Range.Bookmarks(i).Name. The first one always returns the bookmarks in
alphabetical order, the second one in location order. I don't know why that is,
but it works. ;-)
 
S

Shauna Kelly

Hi Jay
Don't bother with the DefaultSorting property, it doesn't help with
this.

You're right. I was confused. Indeed the VBA Help for DefaultSorting
explicitly says "This property doesn't affect the order of Bookmark
objects in the Bookmarks collection."

Cheers

Shauna

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
Z

zain

hi there,

i was trying with both the options but it is not working in vbscript , can
you guess what is the problem here is my code

Function function4(source_file)
Dim objFSO,objWord,objDoc,destination_file,destination_folder,z

'destination_file = "SNIPPET_SEQUENCE.CSV"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWord = CreateObject("Word.Application")

destination_folder = objFSO.GetParentFolderName(source_file)

destinaton_file = destination_folder & "\" & "SNIPPET_SEQUENCE.csv"
'WScript.Echo destinaton_file

objWord.Visible = false

Set objDoc = objWord.Documents.Open(source_file)
'objDoc.Bookmarks.DefaultSorting = wdSortByLocation
Set txt1 = objFSO.CreateTextFile(destinaton_file,ForAppending,true)



'm = objDoc.Bookmarks.count

for x = 1 to objDoc.Bookmarks.count

txt1.WriteLine(objDoc.Range.Bookmarks(x))
txt1.WriteLine(objDoc.Bookmarks(x).range.BookmarkID)

'txt1.WriteLine(objDoc.Bookmarks(x).name)
'txt1.WriteLine(objDoc.Bookmarks(x).range.BookmarkID)

Next

objDoc.close false
txt1.Close
objWord.quit
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top