Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old August 8th, 2003, 05:27 AM
Registered User
Join Date: Aug 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Creating word tables, PLEASE help :D

Ok i have a little problem, i no how to draw a table into a word document from my VB application but i cant seem to get it to draw tables on the fly, i no its somthing silly im doing with the "range" at a guess, hope some one can help me, the following code is just a test but when i get it working ill be able to put it into practace.

Also any help on filling in the table and making the top row merg so that is only has one cell accross the table would be a big help

So it looks kinder like this


Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oRange As Word.Range

oWord = CreateObject("Word.Application")

oWord.Visible = True

oDoc = oWord.Documents.Add

Loop starts=====

oWord.Selection.Tables.Add(oDoc.Range, 4, 2)

Loop Ends =====

Old December 1st, 2003, 08:31 AM
Registered User
Join Date: Nov 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts

Hi Cadey,

Following is the code which fill a single row in the word template. Before running this example create a template and store it in a proper position (Word template). This template should carry a table with number of rows/columns with proper text (template text). In my example i've used ~First~ and ~Last~ words inside my table(row/cell). I am replacing that value with database value. U have got access to all word utilities thru vb.

I don't think is there any possiblity to create table from vb. Anyway try to do the same and let me know once u have acheived it.

Enjoy coding


Dim mobjDoc As Word.Document
Dim con As ADODB.Connection
Dim res As New ADODB.Recordset
Dim strvalue As String
Dim rngRange As Word.Range

Dim objWordApp As Word.Application
mstrReportTemplate = "c:\Test\" & "MyDoc.doc"
Set mobjDoc = GetObject(mstrReportTemplate, "word.document")
Documents.Open ("c:\Test\MyDoc.doc")
Set con = CreateObject("ADODB.Connection")
con.Open "DSN=staples;UID=real_Dev;PWD=its"
res.CursorType = adOpenForwardOnly
res.ActiveConnection = con
res.Open "select co_tnt from res_site_Addl_inf where site_id='0031AZ'", con, , adLockOptimistic
While Not res.EOF
    strFirstName = res.Fields(0)
    strLastName = res.Fields(1)
Set rngRange = mobjDoc.Tables(1).Cell(0, 0).Range
rngHeading.Find.Execute FindText:="~First~", ReplaceWith:=strFirstName
Set rngRange = mobjDoc.Tables(1).Cell(0, 1).Range
rngRange.Find.Execute FindText:="~AFTER~", ReplaceWith:=strLastName


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word tables to access hayk_yer Access VBA 1 June 26th, 2007 10:49 AM
Creating word doc - word behaviour mat41 Classic ASP Professional 2 April 29th, 2007 06:46 PM
creating tables within tables in access??? carswelljr Access 3 August 23rd, 2006 01:21 PM
Help creating tables racin_vegas PHP How-To 1 January 19th, 2006 09:11 AM
Creating Reports in Word mckly VB Databases Basics 1 June 23rd, 2004 05:17 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.