R
RedHeadedMonster via AccessMonster.com
I've got form data that needs to be exported into word. Basically its a
report where the user specifies the time period they want to see for the data.
Im exporting it into a word table. No problem with that have it working
great. However, I want to Insert a TITLE and Time Period above the table.
But not into the header as people use the resulting report to cut and paste
into weekly/monthly reports. How do I do it?
Heres the code I have working
Private Sub cmdCreateCDRLReport_Click()
Dim aWordApp As Word.Application
Dim aRange As Word.Range, aTable As Word.Table
Dim aCell As Word.Cell
Dim iCol As Integer, iRow As Integer
Dim rst1 As DAO.Recordset
Set rst1 = Me.ss_Weekly_MAIN.Form.Recordset
Set aWordApp = CreateObject("Word.Application")
aWordApp.Documents.Add
'create table with data
Set aRange = aWordApp.ActiveDocument.Range(0, 0)
aWordApp.ActiveDocument.Tables.Add Range:=aRange, NumRows:=rst1.RecordCount +
1, NumColumns:=8
aWordApp.Visible = True
'Transfer table column headings
With aWordApp.ActiveDocument.Tables(1).Rows(1)
.Cells(1).Range.Text = "Project"
.Cells(2).Range.Text = "# CDRLs Submitted On-Time"
.Cells(3).Range.Text = "# CDRLs Submitted Late"
.Cells(4).Range.Text = "# CDRLs Approved"
.Cells(5).Range.Text = "# CDRLs Approved w/ Changes"
.Cells(6).Range.Text = "# CDRLs Closed"
.Cells(7).Range.Text = "# CDRLs Disapproved"
.Cells(8).Range.Text = "# CDRLs Awaiting Approval"
End With
'insert data
For iRow = 2 To rst1.RecordCount
iCol = 0
For Each aCell In aWordApp.ActiveDocument.Tables(1).Rows(iRow).Cells
aCell.Range.Text = IIf(rst1.Fields(iCol) > 0, rst1.Fields(iCol), "")
iCol = iCol + 1
Next aCell
rst1.MoveNext
Next iRow
'Set table format
With aWordApp.ActiveDocument.Tables(1)
'Apply formatting
.AutoFormat wdTableFormatClassic2
.AutoFitBehavior wdAutoFitFixed
'Paragraph alignment
.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Range.Font.Name = "Time New Roman"
.Range.Font.Size = 10
End With
'Totals Row
With aWordApp.ActiveDocument.Tables(1).Rows(rst1.RecordCount + 1)
.Cells(1).Range.Text = "TOTALS"
.Cells(2).Range.Text = Me.ss_Weekly_MAIN.Form.Early
.Cells(3).Range.Text = Me.ss_Weekly_MAIN.Form.Late
.Cells(4).Range.Text = Me.ss_Weekly_MAIN.Form.Approved
.Cells(5).Range.Text = Me.ss_Weekly_MAIN.Form.ApprovedC
.Cells(6).Range.Text = Me.ss_Weekly_MAIN.Form.Closed
.Cells(7).Range.Text = Me.ss_Weekly_MAIN.Form.Disapproved
.Cells(8).Range.Text = Me.ss_Weekly_MAIN.Form.Await
End With
End Sub
report where the user specifies the time period they want to see for the data.
Im exporting it into a word table. No problem with that have it working
great. However, I want to Insert a TITLE and Time Period above the table.
But not into the header as people use the resulting report to cut and paste
into weekly/monthly reports. How do I do it?
Heres the code I have working
Private Sub cmdCreateCDRLReport_Click()
Dim aWordApp As Word.Application
Dim aRange As Word.Range, aTable As Word.Table
Dim aCell As Word.Cell
Dim iCol As Integer, iRow As Integer
Dim rst1 As DAO.Recordset
Set rst1 = Me.ss_Weekly_MAIN.Form.Recordset
Set aWordApp = CreateObject("Word.Application")
aWordApp.Documents.Add
'create table with data
Set aRange = aWordApp.ActiveDocument.Range(0, 0)
aWordApp.ActiveDocument.Tables.Add Range:=aRange, NumRows:=rst1.RecordCount +
1, NumColumns:=8
aWordApp.Visible = True
'Transfer table column headings
With aWordApp.ActiveDocument.Tables(1).Rows(1)
.Cells(1).Range.Text = "Project"
.Cells(2).Range.Text = "# CDRLs Submitted On-Time"
.Cells(3).Range.Text = "# CDRLs Submitted Late"
.Cells(4).Range.Text = "# CDRLs Approved"
.Cells(5).Range.Text = "# CDRLs Approved w/ Changes"
.Cells(6).Range.Text = "# CDRLs Closed"
.Cells(7).Range.Text = "# CDRLs Disapproved"
.Cells(8).Range.Text = "# CDRLs Awaiting Approval"
End With
'insert data
For iRow = 2 To rst1.RecordCount
iCol = 0
For Each aCell In aWordApp.ActiveDocument.Tables(1).Rows(iRow).Cells
aCell.Range.Text = IIf(rst1.Fields(iCol) > 0, rst1.Fields(iCol), "")
iCol = iCol + 1
Next aCell
rst1.MoveNext
Next iRow
'Set table format
With aWordApp.ActiveDocument.Tables(1)
'Apply formatting
.AutoFormat wdTableFormatClassic2
.AutoFitBehavior wdAutoFitFixed
'Paragraph alignment
.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Range.Font.Name = "Time New Roman"
.Range.Font.Size = 10
End With
'Totals Row
With aWordApp.ActiveDocument.Tables(1).Rows(rst1.RecordCount + 1)
.Cells(1).Range.Text = "TOTALS"
.Cells(2).Range.Text = Me.ss_Weekly_MAIN.Form.Early
.Cells(3).Range.Text = Me.ss_Weekly_MAIN.Form.Late
.Cells(4).Range.Text = Me.ss_Weekly_MAIN.Form.Approved
.Cells(5).Range.Text = Me.ss_Weekly_MAIN.Form.ApprovedC
.Cells(6).Range.Text = Me.ss_Weekly_MAIN.Form.Closed
.Cells(7).Range.Text = Me.ss_Weekly_MAIN.Form.Disapproved
.Cells(8).Range.Text = Me.ss_Weekly_MAIN.Form.Await
End With
End Sub