Visual Studio Development Bookmark and Share   
 index > Visual Studio Tools for Office > Cell.Range catches more than it's supposed to?
 

Cell.Range catches more than it's supposed to?

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

Tony Hildingstam  Thursday, June 08, 2006 2:03 PM

Hi Tony

Try this variation to make sure the cellrange 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
object objMoveChar = Word.WdUnits.wdCharacter;
object objNeg1Count = (object)-1;
rng.MoveEnd(ref objMoveChar, ref objNeg1Count);
if (rng.Bookmarks.Count > 0)
{

foreach (Word.Bookmark bookie in rng.Bookmarks)
{

cellRowTemplate.Add(
new CellTemplate(bookie, cellCounter));
}
}
}

Cindy Meister  Monday, June 12, 2006 8:21 AM

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 usingRange).

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.

Cindy Meister  Thursday, June 15, 2006 12:32 PM

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.

Cindy Meister  Thursday, June 08, 2006 3:16 PM

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

Tony Hildingstam  Thursday, June 08, 2006 5:21 PM

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?

Tony Hildingstam  Monday, June 12, 2006 6:57 AM

Hi Tony

Try this variation to make sure the cellrange 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
object objMoveChar = Word.WdUnits.wdCharacter;
object objNeg1Count = (object)-1;
rng.MoveEnd(ref objMoveChar, ref objNeg1Count);
if (rng.Bookmarks.Count > 0)
{

foreach (Word.Bookmark bookie in rng.Bookmarks)
{

cellRowTemplate.Add(
new CellTemplate(bookie, cellCounter));
}
}
}

Cindy Meister  Monday, June 12, 2006 8:21 AM

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

Tony Hildingstam  Monday, June 12, 2006 8:42 AM

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 thatI 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?

Tony Hildingstam  Wednesday, June 14, 2006 9:13 AM

Since I didn't get any reply I unmarked the answer to see if anyone can help me.

Tony Hildingstam  Wednesday, June 14, 2006 8:31 PM

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)

Cindy Meister  Thursday, June 15, 2006 10:21 AM

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

Tony Hildingstam  Thursday, June 15, 2006 10:32 AM

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 usingRange).

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.

Cindy Meister  Thursday, June 15, 2006 12:32 PM

A bigTHANKS to you Cindy.

I did not use the Select butcell.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

Tony Hildingstam  Thursday, June 15, 2006 2:01 PM

You can use google to search for other answers

Custom Search

More Threads

• Highlighting the active field in the task pane
• Stack overflow in calling Range.InsertXML()
• visual studio tools for the office 2007 and office suite sp1 not compatible
• Office Word 2003 component in .Net 2005
• Looking for Word Add-In guidance - wide open question
• Word 2003 VSTO SE add-in on Citrix
• F1 help RibbonX custom button
• Best practices for VSTO SE Addins
• insertBeforeMso problem
• Can I develop VSTO 2005 SE plug-ins (Outlook 2003 and Outlook 2007) using Visual Studio 2008 Professional?