Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User defined type not defined- controlling Word from Excel

I am trying to do some relatively simple copy and pasting from Excel 2007 into Word 2007. I've looked through this site and others, and keep getting hung up on the same thing- the third line n the code below keeps giving me the "User type note defined" error msg. I am really confused since I just lifted this from another solution (and had similar issues with other solutions I tried to lift). Could someone please educate me on what is causing the error, and why?

Sub ControlWord()
' **** The line below gives me the error ****
Dim appWD As Word.Application
' Create a new instance of Word & make it visible
Set appWD = CreateObject("Word.Application.12")
appWD.Visible = True

'Find the last row with data in the spreadsheet
FinalRow = Range("A9999").End(xlUp).Row
For i = 1 To FinalRow
    ' Copy the current row
    Worksheets("Sheet1").Rows(i).Copy
    ' Tell Word to create a new document
    appWD.Documents.Add
    ' Tell Word to paste the contents of the clipboard into the new document
    appWD.Selection.Paste
    ' Save the new document with a sequential file name
    appWD.ActiveDocument.SaveAs Filename:="File" & i
    ' Close this new word document
    appWD.ActiveDocument.Close
Next i
' Close the Word application
appWD.Quit
End Sub
like image 389
Tommy Z Avatar asked Jul 30 '12 20:07

Tommy Z


People also ask

How do I fix user-defined Type not defined in VBA?

This error has the following causes and solutions: You tried to declare a variable or argument with an undefined data type or you specified an unknown class or object. Use the Type statement in a module to define a new data type.

What does it mean user-defined Type not defined?

User-defined type | not defined. First, let's try to understand we have encountered the error because something is. “not defined”. A possible reason for the error to occur is that you are utilizing the early binding method to declare and define the object, but the required reference has not been added.

How do you define a user-defined Type in VBA?

The Type statement can be used only at the module level. After you have declared a user-defined type by using the Type statement, you can declare a variable of that type anywhere within the scope of the declaration. Use Dim, Private, Public, ReDim, or Static to declare a variable of a user-defined type.


2 Answers

This answer was mentioned in a comment by Tim Williams.

In order to solve this problem, you have to add the Word object library reference to your project.

Inside the Visual Basic Editor, select Tools then References and scroll down the list until you see Microsoft Word 12.0 Object Library. Check that box and hit Ok.

From that moment, you should have the auto complete enabled when you type Word. to confirm the reference was properly set.

like image 64
ForceMagic Avatar answered Sep 26 '22 02:09

ForceMagic


As per What are the differences between using the New keyword and calling CreateObject in Excel VBA?, either

  • use an untyped variable:

    Dim appWD as Object
    appWD = CreateObject("Word.Application")
    

or

  • Add a reference to Microsoft Word <version> Object Library into the VBA project via Tools->References..., then create a typed variable and initialize it with the VBA New operator:

    Dim appWD as New Word.Application
    

    or

    Dim appWD as Word.Application
    <...>
    Set appWd = New Word.Application
    
    • CreateObject is equivalent to New here, it only introduces code redundancy

A typed variable will give you autocomplete.

like image 26
ivan_pozdeev Avatar answered Sep 23 '22 02:09

ivan_pozdeev