문제

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