C#: Reading a value of a checkbox created with the Control Toolbar

R

Roman F.

Hi,

I am trying to programmatically read (using C#) the value of a checkbox
located within a Word 2003(SP1) document. It appears that the checkbox had
been created with the Control Toolbar (as opposed to Forms); as a result,
Document.FormFields collection is empty. I believe that I need to be somehow
utilizing the Shapes collection to access it (there are white "handles"
around the checkbox), however, so far I've been unable to find a way to see
if the checkbox is checked or not.

Any help would be appreciated.

Thanks,

Roman
 
R

Roman F.

So,

After some time spent banging my head on the wall, I have finally found the
solution to the problem. I just wanted to post it so that someone else
doesn't spend as much time on this as I did.

In order to access a CheckBox that's part of the Shapes collection, the
Microsoft.Forms 2.0 Library has to be added as a reference as the checkbox is
going to be of the Microsoft.Vbe.Interop.Forms.CheckBox type.

See the code snippet below (wordDoc is Word.Document):

// iterate through Word Shapes collection
System.Collections.IEnumerator shapeEnum = wordDoc.Shapes.GetEnumerator();
while (shapeEnum.MoveNext())
{
// check if the shape is a checkbox
if (((Word.Shape)shapeEnum.Current).OLEFormat != null &&
((Word.Shape)shapeEnum.Current).OLEFormat.ClassType.Equals("Forms.CheckBox.1"))
{
Microsoft.Vbe.Interop.Forms.CheckBox cb =
(CheckBox)((Word.Shape)shapeEnum.Current).OLEFormat.Object;
// get_Value() tells if it's checked or not, gets implicitly cast to
string
string CheckBoxAccessInfo = "Got a checkbox" + cb.Caption + " : " +
cb.get_Value() + Environment.NewLine;
}

Let me know if there's a better way to handle this :)
 
C

Cindy M.

Hi Roman,
After some time spent banging my head on the wall, I have finally found the
solution to the problem.
Sorry I wasn't around the last couple of days to help you, after I pointed you
to this group. I was just getting ready to answer when I saw this; I'm glad
you worked out a solution :)
Let me know if there's a better way to handle this :)
Not really better. Out of habit I'd probably use a foreach (Word.Shape shp in
WordDoc.Shapes). I found your IEnumerator approach interesting - not something
I've encountered before :) I've no idea if one is appreciably faster than the
other; I think the foreach would be easier for me to "read".

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
R

Roman F.

Cindy,

I totally think I could've used a foreach. It's definitely more elegant and
readable. For the strangest reason I just didn't think of it (maybe it was
too late at night?).

In any case - you have definitely pointed me in the right direction, and I
appreciate that a lot.

Thank you!

Roman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top