Results 1 to 3 of 3

Thread: Multiple tables on multiple pages

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2024

    Question Multiple tables on multiple pages


    I've been trying to create a word document using that has 1 table per page, and lets say 5 tables, so 5 pages.

    Everything I've tried creates a table and the right number of pages, but only ever writes the table on the first page, so constantly overwrites the previous table.

    I've gone right back to basics with the following, but it still doesn't work.

    My ultimate goal is to run through a CSV of about 100 lines with 4 columns, and create a table per CSV row on each page..

    Code below, thanks.

    Dim csvContent As String() = System.IO.File.ReadAllLines(csvFilePath)
    Dim words As String()
    Dim table As Table = doc.Tables.Add(doc.Range, 2, 2)
    words = csvContent(0).Split(New Char() {","c})
    table.Cell(1, 1).Range.Text = words(0)
    table.Cell(1, 2).Range.Text = words(1)
    table.Cell(2, 1).Range.Text = words(2)
    table.Cell(2, 2).Range.Text = words(3)
    ' Auto fit the table
    Dim table2 As Table = doc.Tables.Add(doc.Range, 2, 2)
    words = csvContent(1).Split(New Char() {","c})
    table2.Cell(1, 1).Range.Text = words(0)
    table2.Cell(1, 2).Range.Text = words(1)
    table2.Cell(2, 1).Range.Text = words(2)
    table2.Cell(2, 2).Range.Text = words(3)

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Chelmsford UK

    Re: Multiple tables on multiple pages

    It looks like they’re all being written on the first page. You specify doc.Range, but I don’t see you’ve changed that anywhere…

  3. #3
    Addicted Member
    Join Date
    Jul 2022

    Re: Multiple tables on multiple pages

    This should get you pretty close to what you're trying to do, add a reference for Microsoft Office and Microsoft Word, Object Libraries.

    Imports System.IO
    Imports Microsoft.Office.Interop
    Public Class Form1
        Dim selectedCSVFileName As String
        Private Sub btnSelectCSVFile_Click(sender As Object, e As EventArgs) Handles btnSelectCSVFile.Click
            ' give the user a file open dialog to select which CSV file to use
            If OpenFileDialog.ShowDialog = DialogResult.OK Then
                selectedCSVFileName = OpenFileDialog.FileName
                selectedCSVFileName = ""
            End If
            txtCSVFileName.Text = selectedCSVFileName
            btnCreateWordDocument.Enabled = selectedCSVFileName.Length > 0
        End Sub
        Private Sub btnCreateWordDocument_Click(sender As Object, e As EventArgs) Handles btnCreateWordDocument.Click
            ' using the CSV file, create a Word document with a page for each record in the CSV file
            Dim wdApp As New Word.Application
            Dim wdDoc As Word.Document
            Dim wdSelection As Word.Selection
            Dim wdRange As Word.Range
            Dim wdTable As Word.Table
            Dim csvFile As StreamReader = File.OpenText(selectedCSVFileName)
            Dim csvLine As String
            Dim csvValues() As String
            Dim csvLineCount As Int16
            Dim tableCellNumber As Int16
            csvLineCount = 1
            wdDoc = wdApp.Documents.Add ' create a new document
            wdSelection = wdApp.Selection
            Do While Not csvFile.EndOfStream
                csvLine = csvFile.ReadLine ' read each line from the file
                csvValues = Split(csvLine, ",") ' create separate values from the line using coma as the delimeter
                If UBound(csvValues) > 0 Then
                    ' there is data to be added to a table in Word
                    If csvLineCount > 1 Then
                        ' create a new page for each line of the csv file
                    End If
                    wdRange = wdSelection.Range
                    wdDoc.Tables.Add(wdRange, 1, UBound(csvValues) + 1)
                    wdTable = wdDoc.Tables(wdDoc.Tables.Count)
                    tableCellNumber = 0
                    For Each cv As String In csvValues
                        wdTable.Cell(1, tableCellNumber).Range.Text = cv
                        tableCellNumber += 1
                    wdSelection.EndKey(6) ' move the cursor to the end
                End If
                csvLineCount += 1
            wdDoc.SaveAs2("f:\temp\test.docx")  ' change this to the file name and location you want
        End Sub
    End Class
    edit: added btn enabled setting depending on file selection or not

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width