'Office Word vba: How to have different alignments in a word table cell?

I want to write in a word some information via vba.

I'm having a problem with the contents of a table I want to import. As you can see my table has 2 columns. The first has the id and the second some descriptions.

I want in the 2nd column the first description to be aligned in the middle and the rest to be aligned to the left.

But I can not do it because everything they will either be aligned in the middle or on the left.

I am giving you two lights to see what I want to achieve and the code I have written

enter image description here

The code is here:

lo_table.cell(i, 2).range.text = ds2.object.main_descr[j]
lo_table.cell(i,2).select()
o1.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
lo_table.cell(i, 2).VerticalAlignment = wdCellAlignVerticalCenter
o1.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
lo_table.cell(i, 2).range.text = lo_table.cell(i, 2).range.text +  main_descr


Solution 1:[1]

You need to apply the default settings for the cell first, add your text, then go back and apply the required settings to the first paragraph. All of this can be done without selecting anything.

If I have understood your rather opaque code correctly you need something like this:

With lo_table.Cell(i, 2)
    .VerticalAlignment = wdCellAlignVerticalCenter
    With .Range
        .ParagraphFormat.Alignment = wdAlignParagraphLeft
        .Text = "add all text to the cell"
        .Paragraphs(1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
    End With
End With

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Timothy Rylatt