如何使用 Range 对象读取 VB.net 中的 Word (*.doc) 文件中复选框的值?

发布于 2024-07-08 21:55:54 字数 325 浏览 5 评论 0原文

如何使用范围对象读取 VB.net 中的单词 (*.doc) 文件中复选框的值?

这就是我到目前为止所拥有的:

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

其中“MyCheckbox”是word文档中复选框的书签。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

握住我的手 2024-07-15 21:55:54

您不使用复选框本身的名称读取复选框的值有什么特殊原因吗?

如果书签定义的范围包含复选框,则根据复选框的插入方式,可以在 InlineShapes 集合(如果复选框与文本内联插入)或 >Shapes 集合(如果作为浮动对象插入。)

然后,您需要遍历 Shapes 或 InlineShapes 集合来查找相关复选框。

迭代 InlineShapes 集合中的控件

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

这应该会让您更接近,但如果复选框的名称是可预测的,则最好直接按名称对其进行寻址。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文