none
VERTICAL ALIGNMENT ON WORD TABLE IS MISS. RRS feed

  • Question

  • Hello everybody,

    Can somebody clear my mind? I did the code below, and this generates a beautiful and clean table on word 2010. The problem is that the instruction “.Rows.Height = 8” and  “.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter” seems  not work, besides, when I try to adjust the rows height and vertical alignment by myself, after routine generates the table of course, Word don´t allow me to do these adjustment by hand.

    The property “.Rows.Height = 8” seems to be working, because when I change to “.Rows.Height = 25” the rows height really goes to it, also, there is no problem with horizontal alignment, this is work very fine.

    Working with VS 2012 and Word 2010.

    Tried everything, no result….

    Thank you.

     Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
            Dim WA As New Word.Application
            Dim WD As Word.Document
            Dim WS As Word.Selection
            Dim model_address, mes_extenso, texto, texto_ref, pic_address As String
            model_address = Application.StartupPath & "\GTMS_MDL.docx"
            pic_address = Application.StartupPath & "\PIC\TABELA01.png"
            WD = WA.Documents.Open(model_address, [ReadOnly]:=True)
            WS = WA.Selection
            Dim WT1 As Word.Table
            Dim WR1 As Word.Range
            Dim lin_wt1, lin_wt2, lin_wt3 As Integer
            lin_wt1 = 3
            WT1 = WD.Tables.Add(WA.Selection.Range, lin_wt1 + 2, 5)
            With WT1.Range
                .ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
                .Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalBottom
                .Rows.Shading.BackgroundPatternColor = Word.WdColor.wdColorAqua
                .Rows(1).Shading.BackgroundPatternColor = Word.WdColor.wdColorBlueGray
                .Rows.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
                .Columns.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
                .Font.Bold = True
                .Font.ColorIndex = Word.WdColorIndex.wdWhite
                .Rows.Height = 8
                .Columns(1).Width = 30
                .Columns(2).Width = 350
                .Columns(3).Width = 40
                .Columns(4).Width = 50
                .Columns(5).Width = 50
                .Font.Size = 7
            End With
            WT1.Range.Text = ""
            WT1.Cell(1, 1).Range.Text = "ITEM"
            WT1.Cell(1, 2).Range.Text = "DESCRIÇÃO"
            WT1.Cell(1, 3).Range.Text = "QTD."
            WT1.Cell(1, 4).Range.Text = "UNIT."
            WT1.Cell(1, 5).Range.Text = "SUBTOTAL"
            For i = 0 To lin_wt1
                WT1.Cell(i + 1, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
            Next
            For i = 0 To lin_wt1 - 1
                WT1.Cell(i + 2, 1).Range.Text = i + 1
                WT1.Cell(i + 2, 2).Range.Text = DGV1.Rows(i).Cells(2).Value
                WT1.Cell(i + 2, 3).Range.Text = DGV1.Rows(i).Cells(3).Value
                WT1.Cell(i + 2, 4).Range.Text = DGV1.Rows(i).Cells(4).Value
                WT1.Rows(i + 2).Range.Font.ColorIndex = Word.WdColorIndex.wdBlack
                WT1.Rows(i + 2).Range.Font.Bold = False
            Next
            WT1.Cell(lin_wt1 + 2, 1).Merge(WT1.Cell(lin_wt1 + 2, 2))
            WT1.Cell(lin_wt1 + 2, 2).Merge(WT1.Cell(lin_wt1 + 2, 4))
            WT1.Cell(lin_wt1 + 2, 1).Range.Text = "TOTAL GERAL"
            WT1.Rows(lin_wt1 + 2).Range.Font.ColorIndex = Word.WdColorIndex.wdDarkRed
            WT1.Rows(lin_wt1 + 2).Range.Font.Size = 10
            WT1.Cell(lin_wt1 + 2, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
            WT1.Cell(lin_wt1 + 2, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
            WT1.Cell(lin_wt1 + 2, 2).Range.Text = FormatCurrency(12560, 2)
            WA.Visible = True
            WA = Nothing
            WD = Nothing
            WS = Nothing
        End Sub
    
    

    • Moved by Tina-Shi Tuesday, April 28, 2015 1:46 AM the issue is related to the office word
    Sunday, April 26, 2015 8:52 PM

Answers

  • Hi,

    >>The problem is that the instruction “.Rows.Height = 8” and  “.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter” seems  not work, besides, when I try to adjust the rows height and vertical alignment by myself, after routine generates the table of course, Word don´t allow me to do these adjustment by hand.

    The property “.Rows.Height = 8” seems to be working, because when I change to “.Rows.Height = 25” the rows height really goes to it, also, there is no problem with horizontal alignment, this is work very fine<<

    I am not able to understand the issue exactly. Can you set the height for the rows using Rows.Height? I made a quick test using VBA based on the code above, it works well for me. And I can also adjust by hand after run the code.

    >> “.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter” seems  not work<<

    What effect did you want to achieve? Based on the code samlpe you were setting the verical alignment to bottom however the description are center. Both center and bottom for the vertical alignment works well for me.

    I would suggest that you test the code using a new document to see whether the issue is realtive to the specific document. Here is the test code in VBA for your reference:

    Sub test()
    Dim WA As New Word.Application
    WA.Visible = True
            Dim WD As Word.Document
            Dim WS As Word.Selection
            Dim model_address, mes_extenso, texto, texto_ref, pic_address As String
            'model_address = Application.StartupPath & "\GTMS_MDL.docx"
            model_address = "C:\doc1.docx"
            'pic_address = Word.Application.StartupPath & "\PIC\TABELA01.png"
    Set WD = WA.Documents.Open(model_address, ReadOnly:=True)
       Set WS = WA.Selection
            Dim WT1 As Word.Table
            Dim WR1 As Word.Range
            Dim lin_wt1, lin_wt2, lin_wt3 As Integer
            lin_wt1 = 3
    Set WT1 = WD.Tables.Add(WA.Selection.Range, lin_wt1 + 2, 5)
            With WT1.Range
                .ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
                .Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter
                .Rows.Shading.BackgroundPatternColor = Word.WdColor.wdColorAqua
                .Rows(1).Shading.BackgroundPatternColor = Word.WdColor.wdColorBlueGray
                .Rows.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
                .Columns.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
                .Font.Bold = True
                .Font.ColorIndex = Word.WdColorIndex.wdWhite
                .Rows.Height = 8
                .Columns(1).Width = 30
                .Columns(2).Width = 350
                .Columns(3).Width = 40
                .Columns(4).Width = 50
                .Columns(5).Width = 50
                .Font.Size = 7
            End With
            WT1.Range.Text = ""
            WT1.Cell(1, 1).Range.Text = "ITEM"
            WT1.Cell(1, 2).Range.Text = "DESCRI??O"
            WT1.Cell(1, 3).Range.Text = "QTD."
            WT1.Cell(1, 4).Range.Text = "UNIT."
            WT1.Cell(1, 5).Range.Text = "SUBTOTAL"
            For i = 0 To lin_wt1
                WT1.Cell(i + 1, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
            Next
            For i = 0 To lin_wt1 - 1
                WT1.Cell(i + 2, 1).Range.Text = i + 1
                WT1.Cell(i + 2, 2).Range.Text = "a" 'DGV1.Rows(i).Cells(2).Value
                WT1.Cell(i + 2, 3).Range.Text = "b" 'DGV1.Rows(i).Cells(3).Value
                WT1.Cell(i + 2, 4).Range.Text = "c" 'DGV1.Rows(i).Cells(4).Value
                WT1.Rows(i + 2).Range.Font.ColorIndex = Word.WdColorIndex.wdBlack
                WT1.Rows(i + 2).Range.Font.Bold = False
            Next
            WT1.Cell(lin_wt1 + 2, 1).Merge WT1.Cell(lin_wt1 + 2, 2)
            WT1.Cell(lin_wt1 + 2, 2).Merge WT1.Cell(lin_wt1 + 2, 4)
            WT1.Cell(lin_wt1 + 2, 1).Range.Text = "TOTAL GERAL"
            WT1.Rows(lin_wt1 + 2).Range.Font.ColorIndex = Word.WdColorIndex.wdDarkRed
            WT1.Rows(lin_wt1 + 2).Range.Font.Size = 10
            WT1.Cell(lin_wt1 + 2, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
            WT1.Cell(lin_wt1 + 2, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
            WT1.Cell(lin_wt1 + 2, 2).Range.Text = FormatCurrency(12560, 2)
            WA.Visible = True
    Set WA = Nothing
       Set WD = Nothing
          Set WS = Nothing
    
    
    End Sub

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, April 29, 2015 5:16 AM
    Moderator

All replies

  • Hi,

    Thank you for posting in MSDN forum.

    Since this issue is related to this Office Word, so we will move this case to this Office Word for developer forum:https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=worddev , you will get better support.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, April 28, 2015 1:45 AM
  • Hi,

    >>The problem is that the instruction “.Rows.Height = 8” and  “.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter” seems  not work, besides, when I try to adjust the rows height and vertical alignment by myself, after routine generates the table of course, Word don´t allow me to do these adjustment by hand.

    The property “.Rows.Height = 8” seems to be working, because when I change to “.Rows.Height = 25” the rows height really goes to it, also, there is no problem with horizontal alignment, this is work very fine<<

    I am not able to understand the issue exactly. Can you set the height for the rows using Rows.Height? I made a quick test using VBA based on the code above, it works well for me. And I can also adjust by hand after run the code.

    >> “.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter” seems  not work<<

    What effect did you want to achieve? Based on the code samlpe you were setting the verical alignment to bottom however the description are center. Both center and bottom for the vertical alignment works well for me.

    I would suggest that you test the code using a new document to see whether the issue is realtive to the specific document. Here is the test code in VBA for your reference:

    Sub test()
    Dim WA As New Word.Application
    WA.Visible = True
            Dim WD As Word.Document
            Dim WS As Word.Selection
            Dim model_address, mes_extenso, texto, texto_ref, pic_address As String
            'model_address = Application.StartupPath & "\GTMS_MDL.docx"
            model_address = "C:\doc1.docx"
            'pic_address = Word.Application.StartupPath & "\PIC\TABELA01.png"
    Set WD = WA.Documents.Open(model_address, ReadOnly:=True)
       Set WS = WA.Selection
            Dim WT1 As Word.Table
            Dim WR1 As Word.Range
            Dim lin_wt1, lin_wt2, lin_wt3 As Integer
            lin_wt1 = 3
    Set WT1 = WD.Tables.Add(WA.Selection.Range, lin_wt1 + 2, 5)
            With WT1.Range
                .ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
                .Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter
                .Rows.Shading.BackgroundPatternColor = Word.WdColor.wdColorAqua
                .Rows(1).Shading.BackgroundPatternColor = Word.WdColor.wdColorBlueGray
                .Rows.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
                .Columns.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
                .Font.Bold = True
                .Font.ColorIndex = Word.WdColorIndex.wdWhite
                .Rows.Height = 8
                .Columns(1).Width = 30
                .Columns(2).Width = 350
                .Columns(3).Width = 40
                .Columns(4).Width = 50
                .Columns(5).Width = 50
                .Font.Size = 7
            End With
            WT1.Range.Text = ""
            WT1.Cell(1, 1).Range.Text = "ITEM"
            WT1.Cell(1, 2).Range.Text = "DESCRI??O"
            WT1.Cell(1, 3).Range.Text = "QTD."
            WT1.Cell(1, 4).Range.Text = "UNIT."
            WT1.Cell(1, 5).Range.Text = "SUBTOTAL"
            For i = 0 To lin_wt1
                WT1.Cell(i + 1, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
            Next
            For i = 0 To lin_wt1 - 1
                WT1.Cell(i + 2, 1).Range.Text = i + 1
                WT1.Cell(i + 2, 2).Range.Text = "a" 'DGV1.Rows(i).Cells(2).Value
                WT1.Cell(i + 2, 3).Range.Text = "b" 'DGV1.Rows(i).Cells(3).Value
                WT1.Cell(i + 2, 4).Range.Text = "c" 'DGV1.Rows(i).Cells(4).Value
                WT1.Rows(i + 2).Range.Font.ColorIndex = Word.WdColorIndex.wdBlack
                WT1.Rows(i + 2).Range.Font.Bold = False
            Next
            WT1.Cell(lin_wt1 + 2, 1).Merge WT1.Cell(lin_wt1 + 2, 2)
            WT1.Cell(lin_wt1 + 2, 2).Merge WT1.Cell(lin_wt1 + 2, 4)
            WT1.Cell(lin_wt1 + 2, 1).Range.Text = "TOTAL GERAL"
            WT1.Rows(lin_wt1 + 2).Range.Font.ColorIndex = Word.WdColorIndex.wdDarkRed
            WT1.Rows(lin_wt1 + 2).Range.Font.Size = 10
            WT1.Cell(lin_wt1 + 2, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
            WT1.Cell(lin_wt1 + 2, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
            WT1.Cell(lin_wt1 + 2, 2).Range.Text = FormatCurrency(12560, 2)
            WA.Visible = True
    Set WA = Nothing
       Set WD = Nothing
          Set WS = Nothing
    
    
    End Sub

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, April 29, 2015 5:16 AM
    Moderator