我要在Word文档中添加文本,并且需要包含下标

时间:2019-12-03 14:59:56

标签: vb.net office-interop

我已经使用单词互操作打开了一个单词文档,并添加了几段文字。它们的长度取决于一系列因素。然后,我在这样的下标中添加一行包含CR的文本(Unicode中不包含字母CR的下标):

            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的两个字符而不是段落变为下标。 如何获取刚刚添加的段落开头的索引?

1 个答案:

答案 0 :(得分:0)

已解决:

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