Вопрос

I have a template word file composed by 2 pages, each page has a bookmark, the first page bookmark name is A4 and the second page bookmark name is A3, but when I read all bookmarks from the word document I get them in alphabetical order, I want them in page order, how can i do this?

foreach (Bookmark bookMark in MergeResultDoc.Bookmarks) 
            {//IMPORTANTE:IL NOME DEL SEGNALIBRO DEVE ESSERE IL TIPO DI CARTA
                pagInizio = Convert.ToInt32(pagNum);
                pagNum = bookMark.Range.Information[WdInformation.wdActiveEndPageNumber].ToString();
                addData( pagInizio, pagNum, bookMark.Name);
                iteration++;
            }
Это было полезно?

Решение 2

Use LINQ OrderBy:

var orderedResults = MergeResultDoc.Bookmarks.OrderBy(d => d.Start).ToList();

Другие советы

You can read the bookMark.Start value. This returns the start position of the Bookmark in the document. So you can run over all Bookmarks and sort them by their start position.

Here is a code to do that:

// List to store all bookmarks sorted by position.
List<Bookmark> bmList = new List<Bookmark>();

// Iterate over all the Bookmarks and add them to the list (unordered).
foreach (Bookmark curBookmark in MergeResultDoc.Bookmarks)
{
    bmList.Add(curBookmark);
}

// Sort the List by the Start member of each Bookmark.
// After this line the bmList will be ordered.
bmList.Sort(delegate(Bookmark bm1, Bookmark bm2)
{
    return bm1.Start.CompareTo(bm2.Start);
});

Document.Boomarks should return the bookmarks in alpha sequence.

Document.Content.Bookmarks should return the bookmarks in the sequence they appear in the document. But VBA collection documentation does not typically guarantee a particular sequence for anything, it's safer to read the Start (as suggested by etaiso) and sort using that.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top