word table with vba

  • Thread starter Thread starter Jesper F
  • Start date Start date
J

Jesper F

I'm trying to create a table in word with vba from access.
I'm using late binding.

This creates a table:
Set myRange = objWord.ActiveDocument.Range(Start:=0,
End:=0)
objWord.ActiveDocument.Tables.Add Range:=myRange,
NumRows:=3, NumColumns:=4

but I'd like to place at a bookmark called "start" in the
document and also be able to dynamically add cells while
looping a recordset.

Does anyone have a small sample of code showing the
principle of placing the table and dynamically adding to
it? I should be able to figure it out then.
Thanks for any help.
 
-----Original Message-----
I'm trying to create a table in word with vba from access.
I'm using late binding.

This creates a table:
Set myRange = objWord.ActiveDocument.Range(Start:=0,
End:=0)
objWord.ActiveDocument.Tables.Add Range:=myRange,
NumRows:=3, NumColumns:=4

but I'd like to place at a bookmark called "start" in the
document and also be able to dynamically add cells while
looping a recordset.

Does anyone have a small sample of code showing the
principle of placing the table and dynamically adding to
it? I should be able to figure it out then.
Thanks for any help.
.
Hi Jesper,
one of the convenient learning methods for Word VB is to
record a macro as you use the application. You can then
view the recording to extract code snippets or to ammend
to suit your requirements.

Try recording 5 macros.
1. Using goto to go to a bookmark location.
2. Inserting a table.
3. Inserting columns.
4. Inserting rows.
5. Moving to a cell and inserting text.

I also suggest that if you require help with programming
in Word that you post to the Word.vba usergroup.

Luck
Jonathan
 
Hi Jesper,

I always prefer to use late binding while I'm writing code. That way,
the VBE's intellisense features list the properties of the Word objects
I'm working with. Then when everything's working right I switch to late
binding and test again, e.g. remove references to Word libraries and
change the declarations:

Early binding:
Dim oWord As Word.Application
Dim oDoc As Word.Document
Late:
Dim oWord As Object 'Word.Application
Dim oDoc As Object 'Word.Document

The advice to record Word macros to see what code is generated is also
good, but you must remember that the macro recorder normally works with
the Word Selection object, and when writing "real" code it's usually
better to define and work with Word Range objects.

To create a table at a bookmark and append rows to it, use something
like

Dim oDoc as Word.Document
Dim oTbl As Word.Table

Set oDoc = ...
....
With oDoc
Set oTbl = .Tables.Add .Bookmarks("MyBookmark").Range, _
1, 4
End With
....

With oTbl
.Rows.Add
With .Rows.Last
.Cells(1).Range.Text = blah blah
.Cells(2).Range.Text = blah blha
...
End With
End WIth
 
I always prefer to use late binding while I'm writing code. That way,
the VBE's intellisense features list the properties of the Word objects
I'm working with.

That should have been "early binding". Late retiring the night before,
no doubt!
 
Back
Top