如何在表格内创建书签

发布于 2024-11-28 18:57:33 字数 868 浏览 0 评论 0原文

我正在自动化 Word 文档处理,其中根据搜索条件添加书签。该代码工作得很好,但当我在文档中有表格时,它会失败。看起来就像在普通文档中,当我阅读文本时,它是按行给出的,但在表格的情况下,文本有列和行。因此,当我搜索文本并且该文本写在单列中的两行中时,结果会没问题,但是当我选择文本 WORD API 时,会从两列中选择文本,而不是同一列中的两行。

col1          col2
This is       Second Column
Some Text

现在,如果我搜索文本“这是一些文本”,我得到了正确的结果,但是当我选择它时,我得到了“这是第二列”

reg = New Regex(result.token(j).ToString())
                Dim m As Match = reg.Match(_doc.Range.Text, 0)
                pos = m.Index ' start position is fine

                ''  start is the starting position of the token in the content...
                ''length is the size of the token
                len = result.token(j).ToString().Length ' text length is fine

                rng = _doc.Range(pos, len + pos) ' this copies the text from the second col
                _doc.Bookmarks.Add(bookmarkName, rng)

I am automating the word document processing where I add bookmarks based on a search criteria. The code works great but it fails when I have tables in the document. It seems like in the normal document when I read the text it is given as line per line but in case of tables the text has columns and rows. So, when I seach a text and that text is written in two lines in a single column the result would be ok but when I select the text WORD API selects the text from two columns instead of the same column but two lines.


col1 col2
This is Second Column
Some Text

Now if I search the text "This is Some text" I got it correctly but when I select it I got "This is Second Column"

reg = New Regex(result.token(j).ToString())
                Dim m As Match = reg.Match(_doc.Range.Text, 0)
                pos = m.Index ' start position is fine

                ''  start is the starting position of the token in the content...
                ''length is the size of the token
                len = result.token(j).ToString().Length ' text length is fine

                rng = _doc.Range(pos, len + pos) ' this copies the text from the second col
                _doc.Bookmarks.Add(bookmarkName, rng)

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

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

发布评论

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

评论(1

日裸衫吸 2024-12-05 18:57:33

您可以选择整个单元格,而不是按长度选择文本,尝试以这种方式调整您的代码:

rng = Selection.Cells(1).Range

instead of selecting the text by length, you can select the whole cell, try to adapt your code this way:

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