Using Office functions question - type mismatch error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • herman404

    #1

    Using Office functions question - type mismatch error

    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("W ord.Application ")
    Set objDoc = objWord.Documen ts.Open(fname)

    objWord.Visible = FALSE

    objDoc.SaveAs Filename+".txt" , wdFormatText

    objWord.Quit
    End function

  • JensB

    #2
    Re: Using Office functions question - type mismatch error


    "herman404" <herman404@hotm ail.com> wrote in message
    news:1147879835 .696974.287660@ j73g2000cwa.goo glegroups.com.. .[color=blue]
    > 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("W ord.Application ")
    > Set objDoc = objWord.Documen ts.Open(fname)
    >
    > objWord.Visible = FALSE
    >
    > objDoc.SaveAs Filename+".txt" , wdFormatText
    >
    > objWord.Quit
    > End function
    >[/color]

    IS this a VB6 App ?
    Jens


    Comment

    • Fred Hedges

      #3
      Re: Using Office functions question - type mismatch error


      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.Documen ts.Open(FileNam e:=CType(fname, Object))


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



      "herman404" <herman404@hotm ail.com> escribió en el mensaje
      news:1147879835 .696974.287660@ j73g2000cwa.goo glegroups.com.. .[color=blue]
      > 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("W ord.Application ")
      > Set objDoc = objWord.Documen ts.Open(fname)
      >
      > objWord.Visible = FALSE
      >
      > objDoc.SaveAs Filename+".txt" , wdFormatText
      >
      > objWord.Quit
      > End function
      >[/color]


      Comment

      • herman404

        #4
        Re: Using Office functions question - type mismatch error

        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#?

        Comment

        • Chris Dunaway

          #5
          Re: Using Office functions question - type mismatch error

          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.

          Comment

          Working...