vb.net - 我正在向 word 文档中添加文本并且需要包含下标
问题描述
我使用 word interop 打开了一个 word 文档,并添加了几段文本。这些长度取决于一系列因素。然后我像这样在下标中添加一行包含 CR 的文本(字母 CR 的下标不包含在 Unicode 下标中):
Dim word As Word.Application = New Word.Application()
Dim doc As Word.Document
'more code inserting paragraphs here
Dim Paratext as string
Paratext = "Elastic Critical Buckling Load N_CR=(π² (E/Yₘ )I)/(L_CR∙L² )"
para0 = doc.Content.Paragraphs.Add
para0.Range.Text = Paratext
para0.Range.InsertParagraphAfter()
para0.Range.Style = doc.Styles("Normal")
Dim Rstart As Int16 = Paratext.IndexOf("CR")
Dim Rend As Int16 = Paratext.IndexOf("=")
' this is wrong: I select the range in the document
' using the location in the paragraph.
Dim SRange = doc.Range(Rstart, Rend)
SRange.Select()
Dim currentselection As Word.Selection = word.Selection
currentselection.Font.Subscript = 1
编辑:我选择了错误的文本。我正在识别刚刚添加的段落中字母 CR 的位置,并将其应用于已经添加了多个段落的文档。所以文档索引 35 和 36 处的两个字符而不是段落成为下标。如何获取刚刚添加的段落开头的索引?
解决方案
解决它:
Dim word As Word.Application = New Word.Application()
Dim doc As Word.Document
'more code inserting paragraphs here
'locate the end of the document so far
Dim DocEnd As Int16 = doc.Content.End - 1
Dim Paratext as string
Paratext = "Elastic Critical Buckling Load N_CR=(π² (E/Yₘ )I)/(L_CR∙L² )"
para0 = doc.Content.Paragraphs.Add
para0.Range.Text = Paratext
para0.Range.InsertParagraphAfter()
para0.Range.Style = doc.Styles("Normal")
Dim Rstart As Int16 = Paratext.IndexOf("CR")
Dim Rend As Int16 = Paratext.IndexOf("=")
Dim SRange = doc.Range(Rstart + DocEnd, Rend + DocEnd)
SRange.Select()
Dim currentselection As Word.Selection = word.Selection
currentselection.Font.Subscript = 1
推荐阅读
- jasper-reports - 在 JasperSoft 服务器中使用 ejbql 数据适配器上传报告
- shell - 匹配 Unix 中的文件字符串
- javascript - 在 javascript 代码中使用 ASP.net 模型。使用以外的任何其他方法
? - html - iframe 遇到问题
- api - 使 Dockerized Flask 服务器并发
- multithreading - Vertx - 创建新线程的调用库
- javascript - 如何检测是否有人在打字但没有发送消息?
- ms-access - MS Access 从两个表中选择
- python - 遍历元组列表一次
- r - 当通过减去 2 个不同的列和 2 个静态对象来使用 Mutate 时,新列是静态的,这是错误的