radekskowron

Hello,

I want to create bookmark in word document - when user selects the text, clicks mouse right button and chooses item form context menu (Add bookmark) the action for this click event fires such code:

Microsoft.Office.Interop.Word.Range rng;

Microsoft.Office.Interop.Word.Bookmark bm;

applicationObject.ActiveDocument.ActiveWindow.View.ShowBookmarks = true;

rng = applicationObject.ActiveDocument.Application.Selection.Range;

Object range = rng;

bm = applicationObject.ActiveDocument.Bookmarks.Add("bm1", ref range);

bm.Application.Selection.FormattedText.Font.Shading.BackgroundPatternColor = (Microsoft.Office.Interop.Word.WdColor)System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(100, 24, 150));//Microsoft.Office.Interop.Word.WdColor.wdColorBrown;

bm.Application.Selection.FormattedText.Font.Color = (Microsoft.Office.Interop.Word.WdColor)System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(10, 240, 50));

It works fine as long as I am selecting text outside table (bookmark start and end markers are in a correct positions), but when I am selecting text that occupies more than one row, column or cell in a table the bookmark's range occupies much more cells (and text) that was selected. For example when I have such a table:

MIESI CZNE CENY PAKIETOW w PLN

Komfortowy Cz onka. Rodziny

Standardowy

Komplekso-wy

-

-

Diamentowy

Brylantowy

Pakiet Indywidualny

-

15,00

*

35,00

*

160,00

*xxxxxxxx

260,00

*

Pakiet dla jednego cz onka rodziny

40,00

*

-

-

-

-

Pakiet Rodzinny

(wspo ma onek/partner oraz dzieci)

55,00

*

75,00

*

-

-

Wspo ma onek / Partner yciowy

-

-

160,00

*

200,00

*

Dziecko (do 25r )

-

-

120,00

*

160,00

*

Rodzic

-

-

260,00

*

340,00

*

Dwoje rodzicow

-

-

440,00

*

600,00

*

and I want to select cells marked orange (2 cells: Diamentowy + xxxxxx) in fact the code provided selects these cells + a few more (like Brylantowy, Pakiet indywidualny,....).

When I am creating this bookmark from the Word UI, it makes this bookmark ranges ok. Where could be the problem Some hints or code examples would be great.

Best Regards



Re: Visual Studio Tools for Office Problem selecting and highlighting text range in table of Word while creating bookmark

Cindy Meister

The behavior you're seeing is due to the fact that, internally Word doesn't store the data in a single column contiguously. Word stores things from top-to-bottom, left-to-right (assuming LTR editing). There's a lot of "magic" going on behind the scenes to let you select cells in a column and treat them as a contiguous selection.

Rather than working with a straight Word-interop Range you'll need to work directly with the Selection. Table columns is one of the exception to the rule of "always use a Range". The following bit of code works for me. Just use sel without Range whereever you want to affect the column

Word.Application wordApp = Marshal.GetActiveObject("Word.Application") as Word.Application;
Word.Selection sel = wordApp.Selection;
Word.Bookmark bm = null;
if (sel.Type == Microsoft.Office.Interop.Word.WdSelectionType.wdSelectionColumn)
{
object oRng = sel.Range;
bm= wordApp.ActiveDocument.Bookmarks.Add("bm1", ref oRng);

sel.Font.Color = (Microsoft.Office.Interop.Word.WdColor)System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(10, 240, 50));
}
bm = null;
sel = null;
wordApp = null;






Re: Visual Studio Tools for Office Problem selecting and highlighting text range in table of Word while creating bookmark

radekskowron

The solution provided by you works a bit better but still the selection after setting bookmark covers two more cells than it should. In this case the extra cells that are selected then are the ones on the right - "Brylantowy" + two cells below "Brylantowy": 260,00 and *. And there is no ending mark "]" of the bookmark visible (the selection goes out of the table). Could it be that the special characters (like cell break markers or whatever formatting signs) make the selection not accurate Writing about "the magic" do you mean a king of a logical concatenation of regions that are not contignous for Word but could be treated as contignous by proper handling in code (like making these splitted ranges to have a common name or whatever that makes them look 'contignous' for the program logic ) If so, is there a kind of a function "IsSelected" that indicates if a specified portion of text is currently selected. I would like to omit hacking solutions if there are a more common ways of handling this problem but maybe that is the only way. Thanks for the responses.





Re: Visual Studio Tools for Office Problem selecting and highlighting text range in table of Word while creating bookmark

Cindy Meister

I did test before I posted, and didn't see any problems. However, I did not test on a table that had merged cells (your first row merges two cells in the top row to make one "header" cell over two columns).

If you could please test on a "standard" table, with no merged cells and see if that is the behavior you're looking for If it is, then the problem you're seeing is probably due to the merged cell you're including in the selection.

If you see the same/similar behavior, which version of Word are you testing against (I did my test with Word 2007)

RE "the magic": I don't know how the Word program is managing this behind the scenes (if I did, I wouldn't call it magic <g>). But you would certainly get more accurate processing by addressing the Cells collection of the Selection. Treat each cell separately, as far as formatting, etc. go. Then you can work with the cell range. This is usually slower, however, than working with the Selection object. Since I don't know exactly what you need these ranges/bookmarks for, it's difficult to make suggestions on how to handle this.

<<is there a kind of a function "IsSelected" that indicates if a specified portion of text is currently selected>>

Do you mean, is it possible to compare the current selection with a known range Yes, you can use the InRange method.






Re: Visual Studio Tools for Office Problem selecting and highlighting text range in table of Word while creating bookmark

radekskowron

You are right, this works ok for simple table. When there are some merged cells it goes wrong. The point is that it works ok when I use standard word menu (when I select the region, click Insert -> Bookmark from the menu bar) then the starting and ending markers of bookmark are set properly. I am using Word 2003.



Re: Visual Studio Tools for Office Problem selecting and highlighting text range in table of Word while creating bookmark

Cindy Meister

Some quick tests in the Word 2003 user interface indicate that you won't be able to get this to work using bookmarks.

1. I can only make the selection ("header cell" + cell below and to the right) by using the keyboard. I can't select it using the mouse.

2. Using "Go To" to pick up the bookmark indicates that it also includes the cell below and to the left, not just the two cells that were selected. This is conforms to Word's internal logic.

3. This bookmark is extremely volatile. If I do anything with the table's structures, it tends to move.

Therefore, using a bookmark with a table structured as you show will not be stable enough to rely on. You'll need to look for alternatives. Why do you require a bookmark






Re: Visual Studio Tools for Office Problem selecting and highlighting text range in table of Word while creating bookmark

radekskowron

In my document I am able to select only these two cells that are marked orange on the picture above (I mean not programmatic selection but manual with mouse). Then when I click Insert->Bookmark from the menu bar, the bookmark is created properly, i.e. only the selected (highlighted) area is marked, and start and end markers are put just on the borders of the selection. The text inside the bookmark covers much more then these two cells - as you wrote - but the selection inside marks only the desired portion of area. I am interested especially in these marked areas as they are interpreted by the user as being selected by him. If there were some methods to identify only these marked areas then it would be ok.

"Why do you require a bookmark "

I need that user can select different areas of the document to make them a kind of fields that he/she will be able to navigate to, change the highlighting color and treat them as fixed areas of the document (to mark important areas of the document). Bookmarks give quite easy interface to make navigation, coloring and are persisted from document to document (after saving the document they are persisted). Maybe there are some different more accurate structures to fullfil my requirements but as I know the Word automation only this solution comes to my mind as the most proper.

Regards,