Link to home
Start Free TrialLog in
Avatar of yassin092898
yassin092898

asked on

Word Programming in VB6

I want to use word to display address informtaion in a database. That is , I want when I click cmdAddress to open the database, open a word document , insert a table of 5 rows and 4 columns in the word document and display the first 5 records in the recordset in the table.
each record in the recordset contains 4 fields. fname, lname, address, phone

I Want word to lok like this.
------------------------------------
|rs!fname|rs!lname|rs!address|rs!Phone|
------------------------------------
|fname2 | lname2| address2| Phone2 |
------------------------------------
|fname3 | lname3| address3| Phone3 |
------------------------------------
|fname4 | lname4| address4| Phone4 |
------------------------------------
|fname5 | lname5| address5| Phone5 |
------------------------------------

Private Sub cmdAddress_click()

Dim db As Database
Dim rs As recordset
Dim strDB As String
strDB = "c:\db1.MDB"
Set db = DBEngine.OpenDatabase(strDB)
Set rs = db.OpenRecordset("Address", dbOpenDynaset

Dim MyWord As Word.Application
Dim MyDoc As Word.document
Set MyWord = New Word.Application

Set MyDoc = MyWord.documents.Add()

add word a table of 5 rows and 4 columns called tble

for i = 1 to i = 5
    MyDoc.tble(i, 1) = rs!fname
    MyDoc.tble(i, 2) =rs!lname
    MyDoc.tble(i, 3) = rs!address
    MyDoc.table(i, 4) = rs!phone
next i


End Sub




Avatar of timytlee
timytlee

'Add a new table (Row x Col)
'at the end of the document.

Sub WordAddTable(TotRow As Integer, TotCol As Integer)

Dim WordRange As Word.Range
Dim LastTable As Integer
   
LastTable = ActiveDocument.Tables.Count
   
Set WordRange = ActiveDocument.Range(ActiveDocument.Range.End - 1, ActiveDocument.Range.End)

ActiveDocument.Tables.Add Range:=WordRange, NumRows:=TotRow, numColumns:=TotCol

End Sub
Avatar of yassin092898

ASKER

Can you rewrite the code I provided so that it accomplishes the following.

-Open the database
-create a word document
-insert a table of 5 rows and 4 columns in the word document
-insert each field of the recodset in the cells of the table in the word document.

The database recordset contains only 5 records each has 4 fields(fname, lname, address, phone)

Here is my first attempt. Note it might not be complete and the syntax is not correct in someplace.


Private Sub cmdAddress_click()

Dim db As Database
Dim rs As recordset
Dim strDB As String
strDB = "c:\db1.MDB"
Set db = DBEngine.OpenDatabase(strDB)
Set rs = db.OpenRecordset("Address", dbOpenDynaset

Dim MyWord As Word.Application
Dim MyDoc As Word.document
Set MyWord = New Word.Application

Set MyDoc = MyWord.documents.Add()

add word a table of 5 rows and 4 columns called tble

for i = 1 to i = 5
    MyDoc.tble(i, 1) = rs!fname
    MyDoc.tble(i, 2) =rs!lname
    MyDoc.tble(i, 3) = rs!address
    MyDoc.table(i, 4) = rs!phone
next i


End Sub

When all is done, I want a word document containing a tablse displaying the fields in the database recordset.

Please note I a beginner in VBA programming.
I don't really understand your solution and I am going to reject.

All I want is a VB Code that
1 - creates a word Document
2 - Inserts a table of 5 rows & 4 columns
3 - put texts in each field of the table in the document.

I would be happy to award you the points and top mark if you are anwer is complete and written for a beginner.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of timytlee
timytlee

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial