如何在表格内创建书签
我正在自动化 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以选择整个单元格,而不是按长度选择文本,尝试以这种方式调整您的代码:
instead of selecting the text by length, you can select the whole cell, try to adapt your code this way: