Exporting data from form & subform to word template

Hi

I have tried to use the code found in this post
http://www.experts-exchange.com/Database/MS_Access/Q_28624031.html
to export data from a form and subform to a word template.

However, I have got a stuck as I get the following error message on the Set Tbl line:
Compile Error: Method or Data Member not found

I have searched and searched for the reason why but haven't been able to pinpoint the problem (I'm sure that it is really obvious to an expert though!)

The code I have used is as follows:

On Error GoTo ErrorHandler
'Print customer Contract for current customer.

    Dim appWord As Word.Application
    Dim docs As Word.Documents
    Dim strLetter As String
    
   Dim strTemplateDir As String
   Dim tbl As Word.Table
    Dim daoRS As DAO.Recordset
  

  Set appWord = GetObject(, "Word.Application")
  
  strTemplateDir = appWord.Options.DefaultFilePath(wdUserTemplatesPath)
   strTemplateDir = strTemplateDir & "\"
   Debug.Print "Office templates directory: " & strTemplateDir
   strLetter = strTemplateDir & "TableTest.dotx"
   Debug.Print "TableTest: " & strLetter
   
Set docs = appWord.Documents
docs.Add strLetter

   With docs
  
  Set daoRS = Forms!ContractFrm!PaymentSubform.Form.RecordsetClone ' needs the names of the main and the sub form in here
               Set tbl = .Tables.Add(.Bookmarks("\EndOfDoc").Range, 1, 3)
        With tbl.Rows.First
            .Cells(1).Range.Text = "PaymentNumber"
            .Cells(2).Range.Text = "DueDateG"
            .Cells(3).Range.Text = "AmountDue"
        End With
        daoRS.MoveFirst
        Do Until daoRS.EOF
            Set rw = tbl.Rows.Add
            With tbl.Rows.First
                .Cells(1).Range.Text = daoRS.Fields("PaymentNumber")
                .Cells(2).Range.Text = daoRS.Fields("DueDateG")
                .Cells(3).Range.Text = daoRS.Fields("AmountDue")
            End With
            daoRS.MoveNext
        Loop
        daoRS.Close
 
    End With

       
   With appWord
      .Visible = True
      .Activate
      .Selection.WholeStory
      .Selection.Fields.Update
      .Selection.MoveDown Unit:=wdLine, Count:=1
   End With
    
    Set docs = Nothing
    Set appWord = Nothing
    
    Exit Sub
    
ErrorHandlerExit:
   Exit Sub

ErrorHandler:
   If Err = 429 Then
      'Word is not running; open Word with CreateObject
      Set appWord = CreateObject("Word.Application")
      Resume Next
   Else
      MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
      Resume ErrorHandlerExit
   End If

Open in new window


I would be really grateful for some help in explaining where I have gone wrong.

For further information the references I have set are:
Visual Basic for Applications
Microsoft Access 14.0 Object Library
OLE Automation
Microsoft ActiveX Data Objects 2.8 Library
Microsoft Word 14.0 Object Library
Microsoft DAO 3.6 Object Library

Thanks so much
N
nrtdAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
You are trying to add a table to the Documents collection. This version declares a document variable, sets it whrn the new document is created and uses it tin the With block:
Sub Q28749879()
On Error GoTo ErrorHandler
'Print customer Contract for current customer.

    Dim appWord As Word.Application
    Dim docs As Word.Documents
    Dim doc As Word.Document 'new
    Dim strLetter As String
    
   Dim strTemplateDir As String
   Dim tbl As Word.Table
    Dim daoRS As DAO.Recordset
  

  Set appWord = GetObject(, "Word.Application")
  
  strTemplateDir = appWord.Options.DefaultFilePath(wdUserTemplatesPath)
   strTemplateDir = strTemplateDir & "\"
   Debug.Print "Office templates directory: " & strTemplateDir
   strLetter = strTemplateDir & "TableTest.dotx"
   Debug.Print "TableTest: " & strLetter
   
Set docs = appWord.Documents
Set doc = docs.Add(strLetter) 'new

   With doc ' was docs
  
  Set daoRS = Forms!ContractFrm!PaymentSubform.Form.RecordsetClone ' needs the names of the main and the sub form in here
               Set tbl = .Tables.Add(.Bookmarks("\EndOfDoc").Range, 1, 3)
        With tbl.Rows.First
            .Cells(1).Range.Text = "PaymentNumber"
            .Cells(2).Range.Text = "DueDateG"
            .Cells(3).Range.Text = "AmountDue"
        End With
        daoRS.MoveFirst
        Do Until daoRS.EOF
            Set rw = tbl.Rows.Add
            With tbl.Rows.First
                .Cells(1).Range.Text = daoRS.Fields("PaymentNumber")
                .Cells(2).Range.Text = daoRS.Fields("DueDateG")
                .Cells(3).Range.Text = daoRS.Fields("AmountDue")
            End With
            daoRS.MoveNext
        Loop
        daoRS.Close
 
    End With

       
   With appWord
      .Visible = True
      .Activate
      .Selection.WholeStory
      .Selection.Fields.Update
      .Selection.MoveDown Unit:=wdLine, Count:=1
   End With
    
    Set docs = Nothing
    Set appWord = Nothing
    
    Exit Sub
    
ErrorHandlerExit:
   Exit Sub

ErrorHandler:
   If Err = 429 Then
      'Word is not running; open Word with CreateObject
      Set appWord = CreateObject("Word.Application")
      Resume Next
   Else
      MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
      Resume ErrorHandlerExit
   End If

                                
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nrtdAuthor Commented:
Fantastic, thanks so much
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.