Link to home
Start Free TrialLog in
Avatar of WhyDidntItWork
WhyDidntItWork

asked on

vb.net creating tables in word documents

Hi,
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.

Thanks.
 
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
            .Workbooks.Open(m_strDirPath)
            .Sheets("Sheet1").activate()
        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
            .Documents.Add()

        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

ASKER CERTIFIED SOLUTION
Avatar of Kalpesh Chhatrala
Kalpesh Chhatrala
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WhyDidntItWork
WhyDidntItWork

ASKER

Had to figure out my own solution.