• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

vb.net creating tables in word documents

I'm trying to create a table in a word document. I want to pass information from Excel to this table in Word. The below code runs fine but it never generates a visible table and I can't figure out why.  Is it the fact that I am using a document.range(0,0)?
Also, where is there good detailed information on the document.range method?  I tried msdn but I didn't find it useful.

Public Class Form1
    Public p_longVariable1 As Long = 0
    Public p_longVariable2 As Long = 0
    Public p_longVariable3 As Long = 0

    'The purpose of this project is to test to see how information can be transferred from an Excel workbook into a table in Word.
    Private m_strDirPath As String = "c:\users\user 02222010\documents\visual studio 2010\projects\wordtest\book2.xlsx"

    Private m_objExcel As New Excel.Application

    Private m_objWord As New Word.Application

    Private m_objDoc As New Word.Document

    Private m_objTable As Word.Table

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Opens up the Excel workbook book2.xlsx.
        With m_objExcel
            .Visible = True
        End With

        'Stores the values from the workbook into the variables.
        p_longVariable1 = m_objExcel.ActiveSheet.Cells(1, 1).value
        p_longVariable2 = m_objExcel.ActiveSheet.Cells(2, 1).value
        p_longVariable3 = m_objExcel.ActiveSheet.Cells(3, 1).value

        'Opens a new instance of Word.
        With m_objWord
            .Visible = True

        End With

        m_objWord.Selection.TypeText("This is a test that we can print to the document.")
        'Tested project to this point!

        m_objTable = m_objDoc.Tables.Add(m_objDoc.Range(0, 0), NumRows:=3, NumColumns:=3)
        'Inserts a table, fill it with data.

        m_objTable.Cell(1, 1).Range.Text = "123"
        m_objTable.Cell(2, 1).Range.Text = p_longVariable2
        m_objTable.Cell(3, 1).Range.Text = p_longVariable3

    End Sub
End Class

Open in new window

1 Solution
Kalpesh ChhatralaSoftware ConsultantCommented:
Check below Sample for Automate Word from VB.Net

WhyDidntItWorkAuthor Commented:
Had to figure out my own solution.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now