Import text into Word document

  • Thread starter RedHeadedMonster via AccessMonster.com
  • Start date
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
 
M

Mark A. Sam

There is probably a method to write directly to the document, but I am not
famililar with exporting to Word to tell you what it is, however it seems to
me that you can insert a 1 row 1 column table first and populate the cell
with the Title and time period. Then set the grid property to transparent.

God Bless,

Mark A. Sam
 
P

Pete D.

Do it manually in word while recording it as a macro. Copy the recorded
macro and paste it in your code. Modify the couple calls to the word
objects using your working table parts as example code, and your good to go.

Mark A. Sam said:
There is probably a method to write directly to the document, but I am not
famililar with exporting to Word to tell you what it is, however it seems
to me that you can insert a 1 row 1 column table first and populate the
cell with the Title and time period. Then set the grid property to
transparent.

God Bless,

Mark A. Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top