list box made by AddItems - how to sort?

M

Michel Peeters

I make listbox " lstBkm" as follows :


For I = 1 To .ActiveDocument.Bookmarks.Count
strBkName = .ActiveDocument.Bookmarks(I).Name
strBkTxt = .ActiveDocument.Bookmarks(I).Range.Text
intPos = .ActiveDocument.Bookmarks(I).Range.BookmarkID
Me.lstBkm.AddItem """" & strBkName & """;""" & strBkTxt &
""";""" & CStr(intPos) & """
Next I


How can I sort it ascending on on intPos?


tks
Michel P.
 
C

Cheryl Fischer

Michel,

I do not know if this will help you or not; however, the Help file for Word
VBA says that bookmarks can be sorted by Location or Name, using the
following constants: wdSortByLocation or wdSortByName

You might want to try the following code, inserted before your loop:

ActiveDocument.Bookmarks.DefaultSorting = wdSortByLocation


hth,
 
C

Cheryl Fischer

Just ran this code through Access 2002/Word XP - and it does work


Dim objWord As Object
Dim strBkName As String
Dim strBkTxt As String
Dim intPos As Integer
Dim i As Integer
Dim iLoop As Integer

' Open Microsoft Word using automation
Set objWord = New Word.Application

objWord.Documents.Add "c:\FormLetter.dot"
objWord.Visible = True
objWord.ActiveDocument.Bookmarks.DefaultSorting = wdSortByLocation

For iLoop = 1 To objWord.ActiveDocument.Bookmarks.Count
For i = 1 To objWord.ActiveDocument.Bookmarks.Count
If objWord.ActiveDocument.Bookmarks(i).Range.BookmarkID = iLoop Then
With objWord.ActiveDocument
'.Bookmarks.DefaultSorting = wdSortByLocation
strBkName = .Bookmarks(i).Name
strBkTxt = .Bookmarks(i).Range.Text
intPos = .Bookmarks(i).Range.BookmarkID
Me.lstBkm.AddItem """" & strBkName & """;""" & strBkTxt &
""";""" & CStr(intPos) & """"
End With
End If
Next i
Next iLoop
 
C

Cheryl Fischer

I could have cleaned that code up a little better:

Dim objWord As Object
Dim strBkName As String
Dim strBkTxt As String
Dim intPos As Integer
Dim i As Integer
Dim iLoop As Integer

' Open Microsoft Word using automation
Set objWord = New Word.Application

objWord.Documents.Add "Path and file name of your document"
objWord.Visible = True
objWord.ActiveDocument.Bookmarks.DefaultSorting = wdSortByLocation

For iLoop = 1 To objWord.ActiveDocument.Bookmarks.Count
For i = 1 To objWord.ActiveDocument.Bookmarks.Count
If objWord.ActiveDocument.Bookmarks(i).Range.BookmarkID = iLoop Then
With objWord.ActiveDocument
strBkName = .Bookmarks(i).Name
strBkTxt = .Bookmarks(i).Range.Text
intPos = .Bookmarks(i).Range.BookmarkID
Me.lstBkm.AddItem """" & strBkName & """;""" & strBkTxt &
""";""" & CStr(intPos) & """"
End With
End If
Next i
Next iLoop
 
M

Michel Peeters

tks, it does work ! - Michel P.

Cheryl Fischer said:
I could have cleaned that code up a little better:

Dim objWord As Object
Dim strBkName As String
Dim strBkTxt As String
Dim intPos As Integer
Dim i As Integer
Dim iLoop As Integer

' Open Microsoft Word using automation
Set objWord = New Word.Application

objWord.Documents.Add "Path and file name of your document"
objWord.Visible = True
objWord.ActiveDocument.Bookmarks.DefaultSorting = wdSortByLocation

For iLoop = 1 To objWord.ActiveDocument.Bookmarks.Count
For i = 1 To objWord.ActiveDocument.Bookmarks.Count
If objWord.ActiveDocument.Bookmarks(i).Range.BookmarkID = iLoop Then
With objWord.ActiveDocument
strBkName = .Bookmarks(i).Name
strBkTxt = .Bookmarks(i).Range.Text
intPos = .Bookmarks(i).Range.BookmarkID
Me.lstBkm.AddItem """" & strBkName & """;""" & strBkTxt &
""";""" & CStr(intPos) & """"
End With
End If
Next i
Next iLoop
 

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