User-defined type not defined / exporting data from Excel to Word template

Emrah EROĞLU 20 Reputation points
2023-06-23T08:46:15.09+00:00

Hi,

I am trying to write a VBA code that transfers data from Excel to Word and saves it as a Word file according to the information in Excel. However, I am taking the error "User-Defined type not defined" every time in line 3 (word.document). Can you please help me to figure out what the error is?

Private Sub CommandButton1_Click()

    Dim WrdApp As Object
    WrdApp = CreateObject("word.application")
    Dim Template As word.document
    
    Row = InputBox("What is the number of the row?")
    ATF_template = Cells(2, 3).Value
    ATF_Save_Location = Cells(3, 3).Value
        
    Set Template = WrdApp.documents.Open(ATF_template, , False)
    WrdApp.Visible = True

        doc.Bookmarks("date").Range.InsertAfter Cells(row, 1)
        doc.Bookmarks("name").Range.InsertAfter Cells(row, 2)
    
    doc.SaveAs2 "ATF_Save_Location" & "ATF-" & Cells(row, 1).Text & "-" & Cells(row, 2).Text 
    Template.Close
    WrdApp.Quit
    
    MsgBox ("Data Sent")

End Sub
Microsoft 365 and Office Word For business Windows
Microsoft 365 and Office Excel For business Windows
0 comments No comments
{count} votes

Accepted answer
  1. Olaf Helper 47,436 Reputation points
    2023-06-23T08:59:04.8533333+00:00

    I am taking the error "User-Defined type not defined" every time in line 3 (word.document).

    Have you set a reference to "Microsoft Word 16.0 Object Library"? By the error I guess the reference is missing.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.