0

I am trying to open word document from the excel but stuck at the first line when I am using the below code. I have added reference still get the compile error user- defined type not defined.

Dim oApp As Word.Application
Dim oSec As Word.Section
Dim oDoc As Word.Document

Set AppWord = CreateObject("Word.Application") 
Set oApp = New Word.Application
Set oDoc = oApp.Documents.Add
Maarten van Stam
  • 1,901
  • 1
  • 11
  • 16
Kavitha
  • 1
  • 1
  • 5
  • Did you tick the Word library in References? – Aeneas Apr 07 '17 at 08:21
  • Possible duplicate of ['User Defined Type Not Defined' error](http://stackoverflow.com/questions/24261557/user-defined-type-not-defined-error) – niton Apr 07 '17 at 10:58

1 Answers1

1

You need to add a reference in the VBA context by opening VBA (Developer Tab, Click: Visual Basic), Select your Workbook and Click from the menu: Tools, References.

In the references list find the Word Object Library and make sure it is checked before clicking OK

enter image description here

Now try again, and don't forget to make oApp visible! :

Sub Test()
   Dim oApp As Word.Application
   Dim oSec As Word.Section
   Dim oDoc As Word.Document

   Set AppWord = CreateObject("Word.Application")
   Set oApp = New Word.Application
   Set oDoc = oApp.Documents.Add

   oApp.Visible = True

End Sub
Maarten van Stam
  • 1,901
  • 1
  • 11
  • 16