View Full Version : Inserting text in a Word doc


PortyAL
06-05-2007, 04:34 AM
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




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

Moniker
06-05-2007, 06:57 AM
You need to control the subform from within your main form. You didn't mention how the subform displays the data (datasheet, textboxes, etc.) but getting to the data is the same.

Me![YourSubformName].Form.<controlnames, etc. here>

If you're showing the subform in datasheet view, you'd cycle through the recordset to get all the data, pseudo-coded here:

With Me![YourSubformName].Form.Recordset
.MoveFirst
While Not .EOF
Send Field1 To Word
Send Field2 To Word
. . . .
Send FieldX To Word
.MoveNext
Wend
End With

The "Send FieldX" lines in the pseudo-code would be something like this:

.Selection.TypeText Text:=.Fields("YourFieldName1")
.Selection.TypeText Text:=.Fields("YourFieldName2")
. . .
.Selection.TypeText Text:=.Fields("YourFieldNameX")

PortyAL
06-05-2007, 07:25 AM
Thanks.

I'll give it a go and report back.

AL

PortyAL
06-06-2007, 02:01 AM
Hi

Having a little bit of bother with this. Getting an error - "invalid use of Me". The data I'm looking to send to word is in a subform called "tbl Control Objectives subform". The field I'm looking in this subform is called "control objective". Is it possible to incorporate the "With Me![YourSubformName].Form.Recordset...." etc. into my code above or does it need to be in a separate routine?

Thanks

AL

PortyAL
06-06-2007, 02:22 AM
Forget the last post. Got it sorted.

Thanks for your help

AL