Kavitha
Kavitha

Reputation: 1

Unable to work with word applications from excel

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

Upvotes: 0

Views: 2889

Answers (1)

Maarten van Stam
Maarten van Stam

Reputation: 1899

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

Upvotes: 1

Related Questions