Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Automation - Multiple Tables

Status
Not open for further replies.

gpalmer711

IS-IT--Management
May 11, 2001
2,445
0
0
GB
Hi,

I have a wierd issue with tables when using word automation in vb.net.

I have the following code

Code:
Imports word = Microsoft.Office.Interop.Word
Public Class frmMain

    Private Sub cmdSettings_Click(sender As Object, e As EventArgs) Handles cmdSettings.Click
        frmSettings.Show(Me)
    End Sub

    Private Sub cmdGenerate_Click(sender As Object, e As EventArgs) Handles cmdGenerate.Click
        Dim objApp = New word.Application
        Dim objDoc = New word.Document

        objApp.Visible = True

        objDoc = objApp.Documents.Add()
        objDoc.Activate()

        objDoc.PageSetup.PageWidth = 294.8
        objDoc.PageSetup.PageHeight = 405.35
        objDoc.PageSetup.RightMargin = 25.5
        objDoc.PageSetup.LeftMargin = 25.5
        objDoc.PageSetup.TopMargin = 25.5
        objDoc.PageSetup.BottomMargin = 28.4

        generateLeftPageTable(objDoc.Bookmarks.Item("\endofdoc").Range)
        generateLeftPageTable(objDoc.Bookmarks.Item("\endofdoc").Range)

    End Sub

    Public Sub generateLeftPageTable(objRange As word.Range)
        Dim objTable As word.Table
        objTable = objRange.Tables.Add(objRange, 5, 1)
        objTable.Borders.Enable = True
        objTable.Borders.OutsideLineStyle = word.WdLineStyle.wdLineStyleNone
        objTable.Borders.InsideLineStyle = word.WdLineStyle.wdLineStyleSingle

        For intRow = 1 To 5
            If intRow = 1 Then
                objTable.Cell(intRow, 1).SetHeight(17, word.WdRowHeightRule.wdRowHeightExactly)
                objTable.Cell(intRow, 1).BottomPadding = 0
                objTable.Cell(intRow, 1).TopPadding = 0
                objTable.Cell(intRow, 1).LeftPadding = 0
                objTable.Cell(intRow, 1).RightPadding = 0
                objTable.Cell(intRow, 1).Borders(word.WdBorderType.wdBorderBottom).LineStyle = word.WdLineStyle.wdLineStyleSingle
                objTable.Cell(intRow, 1).Shading.BackgroundPatternColor = word.WdColor.wdColorBlack
                objTable.Cell(intRow, 1).Range.Text = "Row " & intRow
            Else
                objTable.Cell(intRow, 1).SetHeight(82.2, word.WdRowHeightRule.wdRowHeightExactly)
                objTable.Cell(intRow, 1).BottomPadding = 0
                objTable.Cell(intRow, 1).TopPadding = 0
                objTable.Cell(intRow, 1).LeftPadding = 0
                objTable.Cell(intRow, 1).RightPadding = 0
                objTable.Cell(intRow, 1).Borders(word.WdBorderType.wdBorderBottom).LineStyle = word.WdLineStyle.wdLineStyleSingle
                objTable.Cell(intRow, 1).Range.Text = "Row " & intRow
            End If

        Next

        objTable = Nothing
    End Sub
End Class

I am expecting this to create two separate tables, each with 5 rows - however it creates a single table with 10 rows.

Could someone point me in the right direction?

Greg Palmer
Freeware Utilities for Windows Administrators.
 
I've partially answered this myself

It looks like you cannot have two tables next to each other and they have to be separated by other content, if I simply add a blank paragraph between the tables it works as expected. Abet the layout is not right now as there is a paragraph. I'm sure I can style that though so it has a height of 0.

Code below encase it helps someone else.
Code:
Imports word = Microsoft.Office.Interop.Word
Public Class frmMain

    Private Sub cmdSettings_Click(sender As Object, e As EventArgs) Handles cmdSettings.Click
        frmSettings.Show(Me)
    End Sub

    Private Sub cmdGenerate_Click(sender As Object, e As EventArgs) Handles cmdGenerate.Click
        Dim objApp = New word.Application
        Dim objDoc = New word.Document

        objApp.Visible = True

        objDoc = objApp.Documents.Add()
        objDoc.Activate()

        objDoc.PageSetup.PageWidth = 294.8
        objDoc.PageSetup.PageHeight = 405.35
        objDoc.PageSetup.RightMargin = 25.5
        objDoc.PageSetup.LeftMargin = 25.5
        objDoc.PageSetup.TopMargin = 25.5
        objDoc.PageSetup.BottomMargin = 28.4

        generateLeftPageTable(objDoc.Bookmarks.Item("\endofdoc").Range, objDoc)
        generateLeftPageTable(objDoc.Bookmarks.Item("\endofdoc").Range, objDoc)

    End Sub

    Public Sub generateLeftPageTable(objRange As word.Range, objDoc As word.Document)
        Dim objTable As word.Table
        Dim oPara3 As word.Paragraph

        objTable = objRange.Tables.Add(objRange, 5, 1)
        objTable.Borders.Enable = True
        objTable.Borders.OutsideLineStyle = word.WdLineStyle.wdLineStyleNone
        objTable.Borders.InsideLineStyle = word.WdLineStyle.wdLineStyleSingle

        For intRow = 1 To 5
            If intRow = 1 Then
                objTable.Cell(intRow, 1).SetHeight(17, word.WdRowHeightRule.wdRowHeightExactly)
                objTable.Cell(intRow, 1).BottomPadding = 0
                objTable.Cell(intRow, 1).TopPadding = 0
                objTable.Cell(intRow, 1).LeftPadding = 0
                objTable.Cell(intRow, 1).RightPadding = 0
                objTable.Cell(intRow, 1).Borders(word.WdBorderType.wdBorderBottom).LineStyle = word.WdLineStyle.wdLineStyleSingle
                objTable.Cell(intRow, 1).Shading.BackgroundPatternColor = word.WdColor.wdColorBlack
                objTable.Cell(intRow, 1).Range.Text = "Row " & intRow
            Else
                objTable.Cell(intRow, 1).SetHeight(82.2, word.WdRowHeightRule.wdRowHeightExactly)
                objTable.Cell(intRow, 1).BottomPadding = 0
                objTable.Cell(intRow, 1).TopPadding = 0
                objTable.Cell(intRow, 1).LeftPadding = 0
                objTable.Cell(intRow, 1).RightPadding = 0
                objTable.Cell(intRow, 1).Borders(word.WdBorderType.wdBorderBottom).LineStyle = word.WdLineStyle.wdLineStyleSingle
                objTable.Cell(intRow, 1).Range.Text = "Row " & intRow
            End If

        Next

        oPara3 = objDoc.Content.Paragraphs.Add(objDoc.Bookmarks.Item("\endofdoc").Range)
        oPara3.Range.Text = ""
        oPara3.Range.Font.Bold = False
        oPara3.Format.SpaceAfter = 24
        oPara3.Range.InsertParagraphAfter()
    End Sub
End Class

Greg Palmer
Freeware Utilities for Windows Administrators.
 
One final update - I have replace the new paragraph with a Section Break, which seems to be doing the trick.

Code below encase it helps anyone in the future.

Code:
Imports word = Microsoft.Office.Interop.Word
Public Class frmMain

    Private Sub cmdSettings_Click(sender As Object, e As EventArgs) Handles cmdSettings.Click
        frmSettings.Show(Me)
    End Sub

    Private Sub cmdGenerate_Click(sender As Object, e As EventArgs) Handles cmdGenerate.Click
        Dim objApp = New word.Application
        Dim objDoc = New word.Document
        Dim objPageBreak As Object = word.WdBreakType.wdSectionBreakContinuous

        objApp.Visible = True

        objDoc = objApp.Documents.Add()
        objDoc.Activate()

        objDoc.PageSetup.PageWidth = 294.8
        objDoc.PageSetup.PageHeight = 405.35
        objDoc.PageSetup.RightMargin = 25.5
        objDoc.PageSetup.LeftMargin = 25.5
        objDoc.PageSetup.TopMargin = 25.5
        objDoc.PageSetup.BottomMargin = 28.4

        generateLeftPageTable(objDoc.Bookmarks.Item("\endofdoc").Range, objDoc, objApp)
        objDoc.Bookmarks.Item("\endofdoc").Range.InsertBreak(objPageBreak)
        generateLeftPageTable(objDoc.Bookmarks.Item("\endofdoc").Range, objDoc, objApp)

    End Sub

    Public Sub generateLeftPageTable(objRange As word.Range, objDoc As word.Document, objApp As word.Application)
        Dim objTable As word.Table

        objTable = objRange.Tables.Add(objRange, 5, 1)
        objTable.Borders.Enable = True
        objTable.Borders.OutsideLineStyle = word.WdLineStyle.wdLineStyleNone
        objTable.Borders.InsideLineStyle = word.WdLineStyle.wdLineStyleSingle

        For intRow = 1 To 5
            If intRow = 1 Then
                objTable.Cell(intRow, 1).SetHeight(17, word.WdRowHeightRule.wdRowHeightExactly)
                objTable.Cell(intRow, 1).BottomPadding = 0
                objTable.Cell(intRow, 1).TopPadding = 0
                objTable.Cell(intRow, 1).LeftPadding = 0
                objTable.Cell(intRow, 1).RightPadding = 0
                objTable.Cell(intRow, 1).Borders(word.WdBorderType.wdBorderBottom).LineStyle = word.WdLineStyle.wdLineStyleSingle
                objTable.Cell(intRow, 1).Shading.BackgroundPatternColor = word.WdColor.wdColorBlack
                objTable.Cell(intRow, 1).Range.Text = "Row " & intRow
            Else
                objTable.Cell(intRow, 1).SetHeight(80, word.WdRowHeightRule.wdRowHeightExactly)
                objTable.Cell(intRow, 1).BottomPadding = 0
                objTable.Cell(intRow, 1).TopPadding = 0
                objTable.Cell(intRow, 1).LeftPadding = 0
                objTable.Cell(intRow, 1).RightPadding = 0
                objTable.Cell(intRow, 1).Borders(word.WdBorderType.wdBorderBottom).LineStyle = word.WdLineStyle.wdLineStyleSingle
                objTable.Cell(intRow, 1).Range.Text = "Row " & intRow
            End If

        Next

    End Sub
End Class

Greg Palmer
Freeware Utilities for Windows Administrators.
 
FYI,

If you can't perform in MS Word(manually), it won't work via code either.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top