Word 2003: Programmatically adding one or multiple tables

A

admin

Hello everybody.

I am using Word 2003.

I've ran into a problem trying to print more than one table in the
document.

Here is the picture ( hopefully I'll be able to express it correctly
=) ):
----------------------
- I have a DB from which I'm retrieving some data (cars, for example).
I can retrieve anywhere from 0 to however many of those that come back
after executing my query.
- I am trying to automate the process of adding table(s), separated by
a blank line if there are more than one, to a .doc file. That is
happening (theoretically) in a while loop (While Dr.Read ...)Each
table contains whatever the data is. For example, if it's a car, each
table contains, say, make, model, year, color and vin - some data from
my DB.

Here is what I get:
--------------------
I can add a table, and insert data. The problem is that it's one table
and any attempt of creating a new one fails. I don't know how many
tables I'll end up having in the document, so I cannot statically
create them. When I'm trying to create a new table, the rows are just
added to the end of already created table, or (when I do something
even dumber), nest inside of the first cell.

I've tried numerous ideas that I've got from other posts in here, but
none worked for me. I've been trying to do it for a week now. I would
really appreciate any advice. Thanks in advance for your help and
guidance!
 
D

Doug Robbins - Word MVP

In vba, you could use something like

Dim i As Long
Dim r As Range
Dim t As Table
For i = 1 To 2
Set r = ActiveDocument.Range
r.Collapse wdCollapseEnd
Set t = ActiveDocument.Tables.Add(Range:=r, numRows:=2, numColumns:=2)
Set r = ActiveDocument.Range
r.Collapse wdCollapseEnd
If i < 2 then
r.InsertAfter vbCr
End if
Next i


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
A

admin

Thanks a lot! It worked like a charm! =)

If anybody is still interested in how it's done in VB.NET, here is a
code for the function:

'''''A function that takes one argument as a parameter and returns a
blank string if it's not successful,
''''' or "Success!" string or similar if everything works (for some
weird reason). The method
''''' that calls for this function is designed to catch the return
string and behave accordingly.
Private Function CreateWordDocument(ByVal someParameter As String) As
String
''Trying to opent a Word document, then create tables...
Try
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document

''Visibility is set to true for debugging purposes. I'll
disable it, actually, later on
wrdApp.Visible = True

''The following is an imitation loop. In real application
I'll use DB connection and make it
''a WHILE loop that will go through all the records I need
to display in the tables
Dim i As Integer
Dim r As Word.Range
Dim t As Word.Table

For i = 1 To 2 ''Assuming for right now we only need 2
tables
r = wrdApp.ActiveDocument.Range
r.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
t = wrdApp.ActiveDocument.Tables.Add(Range:=r,
NumRows:=2, NumColumns:=2)

''Insert some data
t.Cell(1, 1).Range.InsertAfter("I'm a cell (1,1),
table number " & i)
t.Cell(2, 1).Range.InsertAfter("I'm a cell (2,1),
table number " & i)

r = wrdApp.ActiveDocument.Range
r.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
If i < 2 Then
r.InsertAfter(vbCr)
End If
Next i

Return "Success!"
Catch ex As Exception
Return ""
End Try

Return ""

end function


Doug, thanks a lot for your help! =)
VJ.
 
P

Perry

Great that with the help of Doug you could make things work
in VB.net, but
'''''A function that takes one argument as a parameter and returns a
blank string if it's not successful,

The way I read yr code, the function will always return ""
 
V

Vassili.King

Perry,

Actually, if the Try statement doesn't fail, after the tables are
created you'll hit the Return "Success" statement which would
terminate the function and return "Success" as a string. If there were
an exception, ther the Return "" would be initiated.
 
P

Perry

It's one out of either ...

If Return statement is facilitated in each branch if the Try/Catch/Finally
block,
it is redundant to use it afterwards ...
Minor point but nevertheless...
Krgrds,
Perry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top