Solved creating tables in word documents

Posted on 2011-04-24
Last Modified: 2012-05-11
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

Question by:WhyDidntItWork
    LVL 16

    Accepted Solution

    Check below Sample for Automate Word from VB.Net

    Author Closing Comment

    Had to figure out my own solution.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now