View Single Post
 
Old 03-29-2012, 08:35 AM
tinfanide tinfanide is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default VBA in Excel: Add a new Word Document on Template (Type Mismatch Error))

Add a new document on an existing template in Excel

Module1:

Code:
Public Sub temp()

''' Early binding
Dim oWord As Word.Application
Set oWord = New Word.Application
oWord.Visible = True
Dim fName As String
fName = "C:\Users\Tin\Desktop\temp1.dotx"
Dim oDoc As Word.Documents
Set oDoc = oWord.Documents.Add(fName) ''' Error: Type Mismatch

''' Late binding NO ERROR
'Dim oWord As Object
'Set oWord = CreateObject("Word.Application")
'oWord.Visible = True
'Dim oDoc As Object
'Dim fName As String
'fName = "C:\Users\Tin\Desktop\temp1.dotx"
'Set oDoc = oWord.Documents.Add(fName)

oDoc.Close
oWord.Quit
Set oDoc = Nothing
Set oWord = Nothing

End Sub
Please see the comments.
I don't know why in early binding adding a new doc on a template causes the type mismatch error, but not in late binding.
Reply With Quote