Hi!
I have a Word document with a table on it. The table consists of two rows and three columns. The first row contains headers and the second row contains bookmarks. There is one bookmark per cell but in the final application there can be zero or more bookmarks per cell.
What i'm trying to do is to get a hold of the bookmark(s) in one cell.
I iterate through the cells like this (C#):
foreach (Microsoft.Office.Interop.Word.Cell cell in searchRange.Tables[1].Rows[2].Cells){
cell.Range.Bookmarks.Count
}
If I then ask the cell how many bookmarks it contains, like above it tells me 3 If I iterate through cell.Range.Bookmarks I get all three bookmarks (and since I am iterating through the cells I get all three bookmarks three times).
Any comments on this
/Tony

Cell.Range catches more than it's supposed to?
Rajiv Kapadia
A big THANKS to you Cindy.
I did not use the Select but cell.Tables[1] instead of cell.Range.Tables[1] and that worked like a charm.
What is this range-thing and why does it never work as I think it should... (don't answer that! ;-)).
Once again: THANKS Cindy!
/Tony
HarryS
Hi Tony
How did you assign the bookmarks Did you select the entire cell, then Insert Or did you click in the cell, then Insert
this sounds like you did the former, which has the effect of saving some of the bookmark information in the binary table structures. It sort of overlaps into everything in the row.
Try clicking IN the cell (blinking cursor) before inserting the bookmarks.
Ravi Sankar Samanthapudi
That did the trick!
It doesn't seem very logical though but i'm happy!
This info should go in to the MSDN-library in the cell.range page.
A very BIG thank you Cindy!
/Tony
XImplosionX
Hi Tony
1. I was away yesterday - yeah, I have to work for my keep, too :-)
2. You shouldn't unmark a correct answer as long as it did answer your original question. Instead, start a new question/thread. Or wait longer for a reponse :-)
3. (In case you're in my time-zone) I will get back to this in a couple of hours (around 1 p.m. GMT if nothing interferes)
Shahzad Ahmed
Hi again!
I now have a new problem that is of the same sort as the one above so I thought I'd post it here rather than creating a new post.
As you can see from the example above I'm iterating through every cell in a table. If I run into a certain bookmark (that starts with "TABLE...")in a cell, the cell should contain another table (nested tables). So now I want to iterate through this nested table.
My approach to this is that I should be able to use Cell.Range.Tables[1] to get the first table residing in the cell. This does not appear to be the case though since Cell.Range.Tables[1] returns the table that the cell itself resides in (the one that i'm currently iterating). Cell.Range.Tables.Count returns 1 so it seems like the only table I can grab is the one I'm getting through Cell.Range.Tables[1].
I have (of course) tried to remove the end-of-cell marker as suggested earlier and I have also tried to move the start of the range one step forward with:
objMoveChar = Word.
WdUnits.wdCharacter; objPos1Count = (object)1;rng.MoveStart(
ref objMoveChar,ref objPos1Count);This does not help. Any suggestions
Alvin B
Since I didn't get any reply I unmarked the answer to see if anyone can help me.
doug2008
Hi Tony
Right, Word's tricky when it comes to nested tables (merged cells are even worse!). Try this: Cell.Tables[1].Select (IOW without using Range).
RE
<<2. OK! I just thought that the two subjects was so related so I thought I'd do the wrong thing if I created a new question (sortof asking the same question twice) and that someone would beat me to death with rubberhoses if I did.>>
Not really a problem; I understand why you did it :-) It's more a question whether others looking for help with the same problem are going to miss the answer if you remove the "correct" flag vs. getting the general public's attention if you don't. Especially since the question isn't directly VSTO-related, you may not have gotten a (techie) response, though, in either case.
Wilke Jansoone
Hi again.
Unfortunately it doesn't matter how I add the bookmarks. The result is still the same (that is; I still get all three bookmarks three times). I have also tried with and without text in the bookmarks but nothing seems to help.
Here's the code (C#):
foreach (Microsoft.Office.Interop.Word.Cell cell in searchRange.Tables[1].Rows[2].Cells){
cellCounter++;
if (cell.Range.Bookmarks.Count > 0){
foreach (Word.Bookmark bookie in cell.Range.Bookmarks){
// This loops three times but should only loop once
cellRowTemplate.Add(
new CellTemplate(bookie, cellCounter));}
}
}Could this be a bug
Bojan
Hi again Cindy!
1. I'm sorry. The comment was not in any way directed to you. It was just a comment that noone had responded. I am extremely grateful for your help.
2. OK! I just thought that the two subjects was so related so I thought I'd do the wrong thing if I created a new question (sortof asking the same question twice) and that someone would beat me to death with rubberhoses if I did.
3. Thanks. I'm looking forward to hearing your ideas on this matter.
/Tony
parcalto
Hi Tony
Try this variation to make sure the cell range doesn't contain that nasty end-of-cell marker:
Word.Range rng = null;
foreach (Microsoft.Office.Interop.Word.Cell cell in searchRange.Tables[1].Rows[2].Cells)
{
cellCounter++;
rng = cell.Range;
//exclude end-of-cell marker
foreach (Word.Bookmark bookie in rng.Bookmarks)object objMoveChar = Word.WdUnits.wdCharacter;
object objNeg1Count = (object)-1;
rng.MoveEnd(ref objMoveChar, ref objNeg1Count);
if (rng.Bookmarks.Count > 0)
{
{
cellRowTemplate.Add(new CellTemplate(bookie, cellCounter));
}
}
}
Ecko
Hi Cindy!
Thanks for the quick reply. I don't remember how I assigned the bookmarks but I will surely try your approach when I get back to work on Monday. I'll try to post here again if it works.
/Tony