Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2007

Tip: Looking for answers? Try searching our database.

Word 2003: Programmatically adding one or multiple tables

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
admin@ex-soviet.com - 02 Feb 2007 19:50 GMT
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!
admin@ex-soviet.com - 02 Feb 2007 19:56 GMT
P.S.: I'm using VB.NET in Studio 2005
Doug Robbins - Word MVP - 03 Feb 2007 18:38 GMT
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

Signature

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

> P.S.: I'm using VB.NET in Studio 2005
admin@ex-soviet.com - 05 Feb 2007 16:01 GMT
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.
Perry - 09 Feb 2007 12:04 GMT
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 ""
--
Krgrds,
Perry

System: Vista/Office Ultimate
> Thanks a lot! It worked like a charm! =)
>
[quoted text clipped - 57 lines]
> Doug, thanks a lot for your help! =)
> VJ.
Vassili.King@gmail.com - 13 Feb 2007 22:36 GMT
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.
Perry - 15 Feb 2007 00:53 GMT
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
> 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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.