INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Word Automation - Multiple Tables

Word Automation - Multiple Tables

(OP)
Hi,

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

I have the following code

CODE --> vb.net

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.
http://www.palmersoft.co.uk

RE: Word Automation - Multiple Tables

(OP)
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 --> VB.NET

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.
http://www.palmersoft.co.uk

RE: Word Automation - Multiple Tables

(OP)
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 --> VB.NET

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.
http://www.palmersoft.co.uk

RE: Word Automation - Multiple Tables

FYI,

If you can't perform in MS Word(manually), it won't work via code either.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close