M
mr_ocp
Hi
I have to automate a report in Word through MS Access, the report is
fairly large and some sections are to be populated from the database I
am using DAO recordset, these sections span over a few pages with data
from access into word tables, what I need to do is to print 33 rows of
recordset to a page and then go to the next page, first print the
section heading, column headings in the top row, then write another 33
rows and so on until the whole recordset has been printed.
Can some one please with necessary code changes to the following
routines
Thanks a bunch.
Public Sub CreateWordTable(rs As DAO.Recordset, strFilePathAndName As
String)
'this code will require a reference to Word
On Error GoTo ProcError
Dim WordObj As Word.Application
Dim oTable As Word.Table
Dim oRange As Word.Range
Dim fld As DAO.Field
Dim f As Integer
Dim c As Integer
Dim r As Integer
'now lets see what we get with Word
On Error Resume Next
'delete file if exists
If Dir(strFilePathAndName) <> "" Then Kill strFilePathAndName
'Attempt to reference Word which may already be running.
Set WordObj = GetObject(, "Word.Application")
If WordObj Is Nothing Then
'Word is not running, better try and start it
Set WordObj = CreateObject("Word.Application")
'Now if WordObj Is Nothing, MS Word is not installed.
If WordObj Is Nothing Then
MsgBox "MS Word is not installed on your computer"
GoTo Exit Here
End If
End If
'reset to regular error handling
On Error GoTo ProcError
With WordObj
.Visible = True
.Documents.Add
Set oRange = .ActiveDocument
Set oTable = ActiveDocument.Tables.Add(oRange, rs.RecordCount,
rs.Fields.Count)
oTable.AutoFormat Format:=wdTableFormatClassic2
Do Until rs.EOF
For c = 1 To oTable.Columns.Count
oTable.Cell(rs.AbsolutePosition, c).Select
.Selection.Text = rs(c - 1)
Next
rs.MoveNext
Loop
.ActiveDocument.SaveAs strFilePathAndName
End With
Exit Here:
WordObj.Documents.Close
WordObj.Quit
Set oTable = Nothing
Set WordObj = Nothing
Exit Sub
ProcError:
Select Case Err.Number
Case Else
MsgBox "Unanticipated error " & Err.Number & " " &
Err.Description & " Aborting!"
Stop
Resume 0 'Hit F8 to goto line with error
End Select
End Sub
I have to automate a report in Word through MS Access, the report is
fairly large and some sections are to be populated from the database I
am using DAO recordset, these sections span over a few pages with data
from access into word tables, what I need to do is to print 33 rows of
recordset to a page and then go to the next page, first print the
section heading, column headings in the top row, then write another 33
rows and so on until the whole recordset has been printed.
Can some one please with necessary code changes to the following
routines
Thanks a bunch.
Public Sub CreateWordTable(rs As DAO.Recordset, strFilePathAndName As
String)
'this code will require a reference to Word
On Error GoTo ProcError
Dim WordObj As Word.Application
Dim oTable As Word.Table
Dim oRange As Word.Range
Dim fld As DAO.Field
Dim f As Integer
Dim c As Integer
Dim r As Integer
'now lets see what we get with Word
On Error Resume Next
'delete file if exists
If Dir(strFilePathAndName) <> "" Then Kill strFilePathAndName
'Attempt to reference Word which may already be running.
Set WordObj = GetObject(, "Word.Application")
If WordObj Is Nothing Then
'Word is not running, better try and start it
Set WordObj = CreateObject("Word.Application")
'Now if WordObj Is Nothing, MS Word is not installed.
If WordObj Is Nothing Then
MsgBox "MS Word is not installed on your computer"
GoTo Exit Here
End If
End If
'reset to regular error handling
On Error GoTo ProcError
With WordObj
.Visible = True
.Documents.Add
Set oRange = .ActiveDocument
Set oTable = ActiveDocument.Tables.Add(oRange, rs.RecordCount,
rs.Fields.Count)
oTable.AutoFormat Format:=wdTableFormatClassic2
Do Until rs.EOF
For c = 1 To oTable.Columns.Count
oTable.Cell(rs.AbsolutePosition, c).Select
.Selection.Text = rs(c - 1)
Next
rs.MoveNext
Loop
.ActiveDocument.SaveAs strFilePathAndName
End With
Exit Here:
WordObj.Documents.Close
WordObj.Quit
Set oTable = Nothing
Set WordObj = Nothing
Exit Sub
ProcError:
Select Case Err.Number
Case Else
MsgBox "Unanticipated error " & Err.Number & " " &
Err.Description & " Aborting!"
Stop
Resume 0 'Hit F8 to goto line with error
End Select
End Sub