[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

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




0
yassin092898
Asked:
yassin092898
  • 2
  • 2
1 Solution
 
timytleeCommented:
'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
0
 
yassin092898Author Commented:
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.
0
 
yassin092898Author Commented:
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
0
 
timytleeCommented:
'well since you are a beginner,
'I hope you understand the code below:

Option Explicit
Dim db As Database
Dim rs As Recordset
Dim MyWord As Word.Application
Dim MyDoc As Word.Document
Dim WordRange As Word.Range
Dim LastTable As Integer
Dim MyTable As Word.Table

Sub WordAddTable(TotRow As Integer, TotCol As Integer)
   
    LastTable = ActiveDocument.Tables.Count
   
    Set WordRange = ActiveDocument.Range(ActiveDocument.Range.End - 1, ActiveDocument.Range.End)
    Set MyTable = ActiveDocument.Tables.Add(Range:=WordRange, NumRows:=TotRow, numColumns:=TotCol)
   
End Sub

Private Sub cmdAddress_click()
Dim i As Integer
'add word a table of 5 rows and 4 columns called tble
WordAddTable 5, 4
For i = 1 To 5
    MyTable.Cell(i, 1).Range.InsertAfter rs!fname
    MyTable.Cell(i, 2).Range.InsertAfter rs!lname
    MyTable.Cell(i, 3).Range.InsertAfter rs!Address
    MyTable.Cell(i, 4).Range.InsertAfter rs!phone
    rs.MoveNext
Next i


End Sub


Private Sub cmdCallWord_Click()
    Set MyWord = CreateObject("Word.Application")
    Set MyDoc = MyWord.Documents.Add
    MyWord.Visible = True
    MyWord.WindowState = wdWindowStateMaximize
End Sub

Private Sub cmdExit_Click()
    rs.Close
    Set rs = Nothing
    db.Close
    Set db = Nothing
    Unload Me
End Sub

Private Sub cmdOpenDb_Click()
Dim strDB As String
strDB = "c:\db1.MDB"
Set db = OpenDatabase(strDB)
Set rs = db.OpenRecordset("Address", dbOpenDynaset)

End Sub
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now