Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Application...Type Mismatch

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi,

I am having a problem with a VB app I am trying to create that should print out a word file.

Here is the code of the offending class:

Option Explicit

Public Sub ShowMessage(ByVal MSG As String)

Dim sFileName As String
Dim iFileNum As Integer
Dim oWord As Word.Document
Dim oWordApp As Word.Application

sFileName = App.Path & "/DCOMTest.htm"

iFileNum = FreeFile
Open sFileName For Output As #iFileNum
Print #iFileNum, MSG
Close #iFileNum

'I have tried both of the following lines and received the same error.
'If I comment out the lines below the program works fine.
'---------------------------------------
Set oWordApp = Word.Application
'Set oWordApp = CreateObject("Word.Application")
'--------This is the offending block----

'Set oWord = oWordApp.Documents.Open(sFileName, False, True, False)
'oWord.PrintOut
'oWord.Close
'Set oWord = Nothing
'Set oWordApp = Nothing

End Sub

This code gives me a type mismatch. Can anyone see why?

Thanks,

Phil Lamey
 
Phil,

I'm not clear which line of code is giving you the Type Mismatch error. Is it Set oWordApp = Word.Application,
Set oWordApp = CreateObject("Word.Application"), or one of the other commented out lines? I have run an equivalent procedure with no problem:

Code:
Sub WorkWithWord()
Dim oWordApp As Word.Application
Dim oWord As Word.Document
Dim sFileName As String

sFileName = "F:\Documents\Virtual_key_codes.doc"
' Either of the next two lines work:
Set oWordApp = Word.Application
' Or
Set oWordApp = CreateObject("Word.Application")

oWordApp.Visible = True  ' So I can see what's going on
Set oWord = oWordApp.Documents.Open(sFileName, False, True, False)
' I set a breakpoint on the next line in order to observe whether my document was loaded; it did. 
oWord.Close  ' I omitted the print step
oWordApp.Quit
Set oWord = Nothing
Set oWordApp = Nothing
End Sub

Disclaimer: I am using VBA from Excel97 not VB. However, the code should behave in a similar, if not identical manner. In order for me to Dim oWordApp as Word.Application and oWord as Word.Document I had to create a reference to the Microsoft Word Object Library.

Regards,
M. Smith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top