Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting data into Word Doc 1

Status
Not open for further replies.

PortyAL

Technical User
May 13, 2005
126
GB
Hi

I've a database of audit jobs.

I'm using the code below to create a Word doc from a template and insert text and various bookmarks based on field values in an open form ("frm Recs Tracked Jobs").

Everything works well, but there is additional info in a subform ("Objectives subform") in the open form which I want to include in the Word doc also. The subform shows the objectives for each job which vary in number. I was wanting to insert the objectives at a bookmark in the Word doc above, but am unsure how to go about it. I tried creating a report and refering to that in the code, but it only inserted the first objective.

Any help would be greatly appreciated.

AL

Code:
sub createdoc()

dim drname as string

drname = Forms![frm recs tracked jobs]![docfolder] & "\" & Forms![frm recs tracked jobs]![Job] & " Draft Report.doc"

Set objword = New Word.Application

    With objword
      .Visible = True
      .Documents.Add Template:=("i:\ia manual\templates\draft report.dot")
      .ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
      .ActiveWindow.ActivePane.View.NextHeaderFooter
      .Selection.TypeText Text:=("DRAFT INTERNAL AUDIT REPORT - " & Forms![frm recs tracked jobs]![Job])
      .ActiveWindow.ActivePane.View.NextHeaderFooter
      .Selection.TypeText Text:=("DRAFT INTERNAL AUDIT REPORT - " & Forms![frm recs tracked jobs]![Job])
      .Selection.Goto Name:=("Audit1")
      .Selection.TypeText Text:=(Forms![frm recs tracked jobs]![Job])
      .Selection.Goto Name:=("Audit2")
      .Selection.TypeText Text:=(Forms![frm recs tracked jobs]![Job])
      .Selection.Goto Name:=("Audit4")
      .Selection.TypeText Text:=(Forms![frm recs tracked jobs]![Job])
      .Selection.Goto Name:=("department")
      .Selection.TypeText Text:=(Forms![frm recs tracked jobs]![DepartmentName])
      .Selection.Goto Name:=("assurancelevel")
      .Selection.TypeText Text:=(Forms![frm recs tracked jobs]![Assurance DR])
      .ActiveDocument.SaveAs (drname)
      .Quit
    End With

End Sub
 
but it only inserted the first objective.

Are you possibly missing a Loop? For instance, do you need to loop through the selected items for sending their respective data to the report?

--

"If to err is human, then I must be some kind of human!" -Me
 
That's probably exactly what I would need, but I wouldn't be an expert in coding.
 
I'm thinking you'd want your loop here:
Code:
    With objword
      .Visible = True
      .Documents.Add Template:=("i:\ia manual\templates\draft report.dot")
      .ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
      .ActiveWindow.ActivePane.View.NextHeaderFooter
      .Selection.TypeText Text:=("DRAFT INTERNAL AUDIT REPORT - "[highlight]
Begin Loop Here (Maybe Loop through a recordset of your form?... something like Do While Not FormRecordSet.EOF )[/highlight]
      strRecords = Forms![frm recs tracked jobs]![Job])
      .ActiveWindow.ActivePane.View.NextHeaderFooter
      .Selection.TypeText Text:=("DRAFT INTERNAL AUDIT REPORT - " & Forms![frm recs tracked jobs]![Job])
      .Selection.Goto Name:=("Audit1")
      .Selection.TypeText Text:=(Forms![frm recs tracked jobs]![Job])
      .Selection.Goto Name:=("Audit2")
      .Selection.TypeText Text:=(Forms![frm recs tracked jobs]![Job])
      .Selection.Goto Name:=("Audit4")
      .Selection.TypeText Text:=(Forms![frm recs tracked jobs]![Job])
      .Selection.Goto Name:=("department")
      .Selection.TypeText Text:=(Forms![frm recs tracked jobs]![DepartmentName])
      .Selection.Goto Name:=("assurancelevel")
      .Selection.TypeText Text:=(Forms![frm recs tracked jobs]![Assurance DR])
[highlight] Loop (End of Loop)[/highlight]
      .ActiveDocument.SaveAs (drname)
      .Quit
    End With

This isn't exactly the typing you would use, but there are examples in the Access VBA help file for looping through records and recordsets. This is assuming your form is displaying one record at a time from it's full record source (such as an underlying table).

--

"If to err is human, then I must be some kind of human!" -Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top