Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to Create a table In Word from Access VBA

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I’m creating a Word 2003 doc from Access 2003. I am using a Word template with bookmarks.
I need to add several tables to display data. What’s the best way to add data to tables?
Example of Table:
Sample Location Quantity
1 Kitchen 230SF
2 Den 300SF
3 Bath 100SF

Here is some VBA code in Access I have so far.
Code:
   Set objWord = CreateObject("Word.Application")
   
   With objWord
       .Visible = True
       .Documents.Open (strDocPath)
       'move to each bookmark, and insert correct text.
       .ActiveDocument.Bookmarks("ProjectNumber").Select
       .Selection.Text = (CStr(Forms!frmReport!txtProjectNumber))
       .ActiveDocument.Bookmarks("WordyStuff").Select
       .Selection.Text = (CStr(Forms!frmReport!txtWordyStuff))

      .ActiveWindow.View.ShowBookmarks = True
   End With

TIA


DougP, MCP, A+
 
1. Start Word
2. Record a macro (Tools - Macro - Record New Macro)
3. Do all the steps 'by hand'
4. Stop Record Macro.
5. Look at the code created (Alt-F11)


Have fun.

---- Andy
 
Thank you, Andrzejek that is a very good idea. I'll try it ina bit.

DougP, MCP, A+
 
Not quite following the connection of bookmarks to creating NEW tables. Or it sounds like you are making new tables.
Code:
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:=3
    Selection.Tables(1).Cell(1, 1).Range.Text = _
        CStr(Forms!frmReport!txtProjectNumber)
Create a 3 x 3 table, and puts CStr(Forms!frmReport!txtProjectNumber) into Cell(1,1).

Perhaps if you clarified precisely what you are trying to do.



Gerry
My paintings and sculpture
 
fumei, Our finshed Word docs have several tables in them whihc I want to fill in using Access. So I am looking for a way to do this using VBA in Access. The Word docs need to be edited and revised so I don't want to use Mail merge. I think the best way is to use bookmarks.

DougP, MCP, A+
 
That does not really answer my question. Are you, or are you not, creating NEW tables? New tables would not have bookmarks.

In any case, you can put stuff into table cells directly, and not only that but yes you can bookmark a table, and then add stuff to table cells directly.
Code:
Dim oTable As Word.Table
Set oTable = _
    ActiveDocument.Bookmarks("MyTable1").Range.Tables(1)
oTable.Cell(2,1).Range.Text = _
    CStr(Forms!frmReport!txtProjectNumber)
which avoids using Selection.

I guess what I am asking is, in what way are you using bookmarks? In your first post you ask:
What’s the best way to add data to tables?
The answer to that is...directly.

The best way to add data to tables is to add it directly to the cell you want to put it in. You do not need to use any bookmarks to do this.

I would further point out that:
Code:
.ActiveDocument.Bookmarks("ProjectNumber").Select
    .Selection.Text = (CStr(Forms!frmReport!txtProjectNumber))
does NOT actually put that text into the bookmark. To do so you need to recreate the bookmark, as in:
Code:
.ActiveDocument.Bookmarks("ProjectNumber").Select
    .Selection.Text = (CStr(Forms!frmReport!txtProjectNumber))
.ActiveDocument.Bookmarks.Add Name:="ProjectNumber", _
    Range:=Selection.Range
The advantage of this is that if you ever want to get the data OUT, then if it is actually IN the bookmark, getting the value is a snap.

Code:
ActiveDocument.Bookmark("ProjectNumber").Range.Text
will gets that data. Without recreating the bookmark after you use Selection.Text, the bookmark will not contain the data you used.

However, you do not have to make any selecting at all to use the bookmarks. You can action them directly as well.

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top