Inserting text in a Word doc

PortyAL

Craigavon, N.Ireland
Local time
Today, 09:02
Joined
Apr 16, 2007
Messages
24
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
 
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:

Code:
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:

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

I'll give it a go and report back.

AL
 
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
 
Forget the last post. Got it sorted.

Thanks for your help

AL
 

Users who are viewing this thread

Back
Top Bottom