Share via

Compile error User Defined Type not defined in module to export data to MS Word

Anonymous
2017-03-23T17:18:07+00:00

This module literally worked fantastically yesterday.  In the meantime I upgraded my Access subscription to Office 365 Business and had to have Access reinstalled.  I am now getting the error "Compile error User Defined Type not defined" on a module that merges access data to a word document.  Here is the code:

Public Sub ExportDatatoEASETemplate()

Dim wApp As Word.Application

        Dim wDoc As Word.Document

        Dim rs As DAO.Recordset

    Set wApp = New Word.Application

    Set wDoc = wApp.Documents.Open("C:\Users\Robert\Documents\MPN\Proposal Template.docx")

    Set rs = CurrentDb.OpenRecordset("qryFirstNationalForm")

    If Not rs.EOF Then rs.MoveFirst

    Do Until rs.EOF

    wDoc.Bookmarks("CorporateName").Range.Text = Nz(rs!CorporateName, "")

    wDoc.Bookmarks("Address1").Range.Text = Nz(rs!Address1, "")

    wDoc.Bookmarks("Address2").Range.Text = Nz(rs!Address2, "")

    wDoc.Bookmarks("City").Range.Text = Nz(rs!City, "")

    wDoc.Bookmarks("Province").Range.Text = Nz(rs!Province, "")

    wDoc.Bookmarks("PostalCode").Range.Text = Nz(rs!PostalCode, "")

    wDoc.SaveAs2 "C:\Users\Robert\Documents\MPN" & rs!FirstNationalID & "_Proposal Template.docx"

    wDoc.Bookmarks("CorporateName").Range.Delete wdCharacter, Len(Nz(rs!CorporateName, ""))

    wDoc.Bookmarks("Address1").Range.Delete wdCharacter, Len(Nz(rs!Address1, ""))

    wDoc.Bookmarks("Address2").Range.Delete wdCharacter, Len(Nz(rs!Address2, ""))

    wDoc.Bookmarks("City").Range.Delete wdCharacter, Len(Nz(rs!City, ""))

    wDoc.Bookmarks("Province").Range.Delete wdCharacter, Len(Nz(rs!Province, ""))

    wDoc.Bookmarks("PostalCode").Range.Delete wdCharacter, Len(Nz(rs!PostalCode, ""))

        rs.MoveNext

    Loop

    wDoc.Close False

    wApp.Quit

    Set wDoc = Nothing

    Set wApp = Nothing

    Set rs = Nothing

End Sub

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Most helpful
  1. HansV 462.6K Reputation points
    2017-03-23T17:31:14+00:00

    Select Tools > References... in the Visual Basic Editor.

    Tick the check box for 'Microsoft Word 16.0 Object Library'.

    Click OK.

    Was this answer helpful?

    4 people found this answer helpful.
    0 comments No comments
  2. HansV 462.6K Reputation points
    2017-03-23T22:16:27+00:00

    Also set a reference to DAO:

    If your database is a .mdb database, it is the Microsoft DAO 3.6 Object Library.

    If your database is a .accdb database, it is the Microsoft Office 16.0 Access database engine Object Library.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2017-03-23T22:15:48+00:00

    Solved it Hans - had to add Microsoft DAO to references.  Thanks for your help!!

    Rob

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2017-03-23T22:06:38+00:00

    Thanks Hans - I did do that however still getting the same error. Not sure if it means anything but this is how access highlights the error in the code:

    Was this answer helpful?

    0 comments No comments