VSTO 2005 Beta2 Bookmark Relocate
I created a C# VSTO project in Beta 2 and built a simple program that uses a TreeView in the ActionsPane. The tree view contains nodes representing the Bookmark view controls in the Word document.
I implemented the Drag/Drop capabilities within the TreeView and want to reflect the changes in the Word document. I am having some problems with calling the Relocate() method on the bookmarks. After re-ordering the TreeView I must make successive calls to Relocate() on the corresponding Bookmark. Unfortunately, I am getting inconsistent results with Word.
Sometimes it works but other times the Bookmark brackets inside of Word have lost an adjacent Bookmark ending bracket.
So, I go from something that looks like this (3 visual bookmarks):
[Bookmark1]
[Bookmark2]
[Bookmark3]
to this (2 visual bookmarks):
[Bookmark3
Bookmark1]
[Bookmark2]
Any ideas?
Hi Bill,
The issue that you are seeing when relocating the bookmarks isn't specific to VSTO 2005 as I was able to reproduce the issue by relocating the bookmark range within Word VBA.
One possible solution that you may be able to implement is to insert a paragraph before or after, depending upon the relocation direction of the bookmark, the bookmark which is adjacent to the bookmark that you are moving so that when you move the bookmark it is being moved into an empty paragraph and then delete the paragraph once the relocation process is complete.
This may not be an easy task given that you would need to determine 1) that you were about to relocate to a paragraph containing an existing bookmark and 2) you would need to obtain a reference to the bookmark directly before or after the bookmark that you are moving in order to insert the paragraph mark in the correct location.
If you have any questions please let me know via the posting.
Regards,
Ken Laws
MSFT
This posting is provided "AS IS" with no warranties, and confers no rights.
For more information regarding Visual Studio Tools for Office 2005:
Best of Blogs: Visual Studio 2005 Tools for Office
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_2003_ta/html/odc_landvsto2005_ta.asp
Visual Studio Tools for Office Forum
http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=16
Visual Studio Tools for the Microsoft Office System
http://msdn.microsoft.com/office/understanding/vsto/default.aspx
Here's a workaround that works for my specific problem:
| | Microsoft.Office.Tools.Word. Bookmark OuterBookmark; Microsoft.Office.Tools.Word. Bookmark InnerBookmark; for (int i = 0; i < BookmarkCount; i++) { OuterBookmark = (Microsoft.Office.Tools.Word. Bookmark) Controls [ i ]; for (int j = i+1; j < BookmarkCount; j++) { InnerBookmark = (Microsoft.Office.Tools.Word. Bookmark) Controls [ j ]; if ( OuterBookmark.Range.Start == InnerBookmark.Range.Start ) { if (OuterBookmark.Range.Characters.Count > InnerBookmark.Range.Characters.Count) OuterBookmark.SetRange(InnerBookmark.Range.End, OuterBookmark.Range.End); else InnerBookmark.SetRange(OuterBookmark.Range.End, InnerBookmark.Range.End); i = j = BookmarkCount; } } } |