Home All Groups Group Topic Archive Search About

Using Office functions question - type mismatch error

Author
17 May 2006 3:30 PM
herman404
Hi everyone, I am trying to write a function that will be called from
another function.  It will do a save as on a word document, and the
document name will be passed via a parameter.  I keep getting a Type
Mismatch error on the Documents.Open call below, but it works fine when
I specify the name of the file in the Open call.  How come it doesn't
work with the parameter, even when I declare a local variable and set
it equal to the parameter?  The parameter is confirmed as being a
string using vartype.


Function ConvertWordFile(Filename)
    Const wdFormatText = 2
    Dim fname
    Dim objWord
    Dim objDoc

    Set fname = Filename
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Open(fname)

    objWord.Visible = FALSE

    objDoc.SaveAs Filename+".txt", wdFormatText

    objWord.Quit
End function

Author
17 May 2006 3:45 PM
Fred Hedges
The caller is expecting an "Object", whereas you are passing a String.
Don't ask me why it won't work, but rest assured if you cast the string to
an Object, it'll be okay, such as:


objDoc = objWord.Documents.Open(FileName:=CType(fname, Object))


Remember to "Release" these interfaces when you are done with them,
preferably in an exception handler "Finally" statement.



Show quoteHide quote
"herman404" <herman***@hotmail.com> escribió en el mensaje
news:1147879835.696974.287660@j73g2000cwa.googlegroups.com...
> Hi everyone, I am trying to write a function that will be called from
> another function.  It will do a save as on a word document, and the
> document name will be passed via a parameter.  I keep getting a Type
> Mismatch error on the Documents.Open call below, but it works fine when
> I specify the name of the file in the Open call.  How come it doesn't
> work with the parameter, even when I declare a local variable and set
> it equal to the parameter?  The parameter is confirmed as being a
> string using vartype.
>
>
> Function ConvertWordFile(Filename)
> Const wdFormatText = 2
> Dim fname
> Dim objWord
> Dim objDoc
>
> Set fname = Filename
> Set objWord = CreateObject("Word.Application")
> Set objDoc = objWord.Documents.Open(fname)
>
> objWord.Visible = FALSE
>
> objDoc.SaveAs Filename+".txt", wdFormatText
>
> objWord.Quit
> End function
>
Author
17 May 2006 4:54 PM
herman404
Hi Fred, thanks for the tip, that makes sense.  However, I am getting
syntax errors on the casting, and I can't figure out what's wrong (I am
new to VB, by the way, as I usually write in Java and C#)  It returns a
syntax error on the CType call, saying that the object type is not
recgonised.  The CObj data type does not work either.  A search on
another page mentioned calling CType with a variable of the type rather
than the name of the type.  But when I try to do a:

Dim obj As Object

The interpreter marks a syntax error on the As!  Is there any way to do
this, or should I just give up and write this in C#?
Author
18 May 2006 1:30 PM
Chris Dunaway
It sounds like you are writing this in VBA and not VB.Net.  I don't
think VBA supports the As clause and it certainly does not support
CType.
Author
17 May 2006 3:52 PM
JensB
Show quote Hide quote
"herman404" <herman***@hotmail.com> wrote in message
news:1147879835.696974.287660@j73g2000cwa.googlegroups.com...
> Hi everyone, I am trying to write a function that will be called from
> another function.  It will do a save as on a word document, and the
> document name will be passed via a parameter.  I keep getting a Type
> Mismatch error on the Documents.Open call below, but it works fine when
> I specify the name of the file in the Open call.  How come it doesn't
> work with the parameter, even when I declare a local variable and set
> it equal to the parameter?  The parameter is confirmed as being a
> string using vartype.
>
>
> Function ConvertWordFile(Filename)
> Const wdFormatText = 2
> Dim fname
> Dim objWord
> Dim objDoc
>
> Set fname = Filename
> Set objWord = CreateObject("Word.Application")
> Set objDoc = objWord.Documents.Open(fname)
>
> objWord.Visible = FALSE
>
> objDoc.SaveAs Filename+".txt", wdFormatText
>
> objWord.Quit
> End function
>

IS this a VB6 App ?
Jens