T Surafel
T Surafel

Reputation: 3

How to write two equations in different lines in word from vb.net

Am working on automating a word document from VB.net and am unable to write an equation in a separate two different lines. The code I have is here, it will write the equations in single line (merged in one), but I want them in different lines please help.

am using word 2016 and vb.net 2017

    Imports Microsoft.Office.Interop.Word

    '**************Intializing Word Document****************************
    Dim oWord As Microsoft.Office.Interop.Word.Application
    Dim oDoc As Microsoft.Office.Interop.Word.Document
    'Start Word and open the document template.
    oWord = CreateObject("Word.Application")
    oWord.Visible = True
    oDoc = oWord.Documents.Add
    '*******************************************************************

    '**************************eq 1*****************************************
    Dim objRange As Range
    Dim objEq As OMath
    objRange = oDoc.Bookmarks.Item("\endofdoc").Range
    objRange.InsertParagraphAfter()
    objRange.Text = "σ_1 (L_x/2,L_y/2)=N_sd/(L_x L_y )+(M_x (L_y/2))/I_x +(M_y (L_x/2))/I_y  = "
    objRange = oDoc.OMaths.Add(objRange)
    objEq = objRange.OMaths(1)
    objEq.BuildUp()
    '**************************eq 2*****************************************
    Dim objRange1 As Range
    Dim objEq1 As OMath
    objRange1 = oDoc.Bookmarks.Item("\endofdoc").Range
    objRange1.InsertParagraphAfter()
    objRange1.Text = "σ_2 (L_x/2,L_y/2)=N_sd/(L_x L_y )+(M_x (L_y/2))/I_x +(M_y (L_x/2))/I_y  = "
    objRange1 = oDoc.OMaths.Add(objRange1)
    objEq1 = objRange1.OMaths(1)
    objEq1.BuildUp()

Upvotes: 0

Views: 517

Answers (1)

soohoonigan
soohoonigan

Reputation: 2360

The problem is a slight mismanagement of the ranges and oMaths collection. You don't need a second range, just need to shift the first one. After you set the range's initial position, just enter the equation on the first line. Then, you insert a paragraph and call insertafter to add another equation to the document. Insertafter will automatically set the range's value to the newly added text. Then simply add that second range to the omaths collection, making its count 2. Finally, call buildup on the entire collection and it will build both equations.

With those changes applied, you get the following code which produces two equations on different lines:

    '**************Intializing Word Document****************************
    Dim oWord As Microsoft.Office.Interop.Word.Application
    Dim oDoc As Microsoft.Office.Interop.Word.Document
    'Start Word and open the document template.
    oWord = CreateObject("Word.Application")
    oWord.Visible = True
    oDoc = oWord.Documents.Add

    '**************************eq 1*****************************************
    Dim objRange As Range
    objRange = oDoc.Bookmarks.Item("\endofdoc").Range
    objRange.Text = "σ_1 (L_x/2,L_y/2)=N_sd/(L_x L_y )+(M_x (L_y/2))/I_x +(M_y (L_x/2))/I_y  = "
    objRange = oDoc.OMaths.Add(objRange)
    '**************************eq 2*****************************************
    objRange = oDoc.Bookmarks.Item("\endofdoc").Range
    objRange.InsertParagraphAfter()
    objRange.InsertAfter("σ_2 (L_x/2,L_y/2)=N_sd/(L_x L_y )+(M_x (L_y/2))/I_x +(M_y (L_x/2))/I_y  = ")
    objRange = oDoc.OMaths.Add(objRange)
    objRange.OMaths.BuildUp()

Upvotes: 1

Related Questions