Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read the value of a checkbox in a word (*.doc) file in VB.net using a Range object?

How do I read the value of a checkbox in a word (*.doc) file in VB.net using a range object?

This is what I have so far:

Dim app As New Word.Application
Dim doc As Document
doc = app.Documents.Open("C:\myDoc.doc")
dim chkBox as Bookmark
chkBox = doc.Bookmarks("MyCheckbox")
Dim rng as Range
rng = chkBox.Range

where "MyCheckbox" is the bookmark of the checkbox in the word document.

like image 488
wusher Avatar asked Nov 15 '22 16:11

wusher


1 Answers

Any particular reason you're not reading the value of the checkbox using the name of the checkbox itself?

If the range defined by your bookmark contains a checkbox, then, depending upon how the checkbox is inserted, it will be found in either the InlineShapes collection (if checkbox inserted inline with the text) or the Shapes collection (if inserted as a floating object.)

You would then need to iterate through the collection of Shapes or InlineShapes looking for the checkbox in question.

Iterating through controls in InlineShapes collection

Dim ctl As InlineShape
For Each ctl In rng.InlineShapes
    If ctl.Type = wdInlineShapeOLEControlObject Then
        If ctl.OLEFormat.ClassType Like "Forms.CheckBox*" Then
            'current version of ctl is a checkbox, do what you will with it.

        End If
    End If
Next ctl
...

This ought to get you closer, but if the name of the checkbox is predictable, it is better to address it directly by name.

like image 185
HipCzeck Avatar answered Jan 11 '23 20:01

HipCzeck