Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

"type mismatch" in Word

by esr (Scribe)
on Nov 30, 2004 at 03:39 UTC ( #411091=perlquestion: print w/replies, xml ) Need Help??
esr has asked for the wisdom of the Perl Monks concerning the following question:

I am using Win32::OLE to interact with Word and I am getting an error which I can't see the reason for. The Perl code is opening a .txt file, running a macro, and then trying to save the file as a Word document (.doc). The SaveAs call precipitates an error which says: Win32::OLE<0.1603> error 0x80020005: "Type mismatch" in METHOD/PROPERTYGET "SaveAs" argument 4 in blahblah line 1318. As near as I can tell, the code I have written is the same as examples I have seen of how to read a text file and save as a Word document. The code snippet is as follows:
use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; $word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application'); $mydoc = $word->Documents->Open($xfile); $word->Run("tskedfmt"); ($xfn) = $xfile =~ m/(.*)\.txt$/; $xfn .= "\.doc"; $mydoc->SaveAs(FileName => "$xfn", FileFormat => wdFormatDocument); $word->{Visible} = 1;
What am I missing?

Replies are listed 'Best First'.
Re: "type mismatch" in Word
by bmann (Priest) on Nov 30, 2004 at 05:34 UTC
    If you want to use named parameters with Office and OLE, use a hashref.

    $mydoc->SaveAs( {FileName => "$xfn", FileFormat => wdFormatDocument} );

Re: "type mismatch" in Word
by diotalevi (Canon) on Nov 30, 2004 at 04:23 UTC

    Perhaps you can't use named parameters like that. I looked up the positional arguments for .SaveAs and the first two are FileName and FileFormat. Try that again as $mydoc->SaveAs( $xfn, 'wdFormatDocument' ).

Re: "type mismatch" in Word
by tachyon (Chancellor) on Nov 30, 2004 at 10:19 UTC

    As noted you need to pass a hash ref. There is also another method that works....

    my $doc = $word->{Documents}->Open($infile); # either use this $doc->SaveAs( { FileName => $outfile, FileFormat => wdFormatText } ); # or you can do this $word->WordBasic->FileSaveAs($outfile);



Re: "type mismatch" in Word
by fglock (Vicar) on Nov 30, 2004 at 20:41 UTC

    See this: FIX: Type Mismatch Using OLE Automation to Word 97

    ... some of the methods used in Word 97 VBA return errors back to the client application. When an error is returned to the client application, ... , it may be necessary to use older Word Basic statements and functions in conjunction with the newer Word.Application methods and properties. ...

    They give some examples on how to do it.

Re: "type mismatch" in Word
by jimbojones (Friar) on Nov 30, 2004 at 20:36 UTC
    I ran your code, without the macro, and with the hashref, and it saved correctly as a Word binary document. I also fixed the line $xfn .= "\.doc"

    use Win32::OLE; use Win32::OLE::Const 'Microsoft Word';; my $xfile = "C:/Documents and Settings/jim/My Documents/perlmonks_exam +ples/temp.txt"; $word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application'); $mydoc = $word->Documents->Open($xfile); #$word->Run("tskedfmt"); ($xfn) = $xfile =~ m/(.*)\.txt$/; $xfn .= ".doc"; $mydoc->SaveAs( {FileName => "$xfn", FileFormat => wdFormatDocument} ) +; $word->{Visible} = 1;
    - j
Re: "type mismatch" in Word
by esr (Scribe) on Nov 30, 2004 at 19:51 UTC
    The previous two responses were helpful and led to some further experimentation. The hash reference is definitely needed but it did not get rid of the "type mismatch" error. It is the FileFormat parameter that is causing the error msg. The SaveAs works without the error message if I omit the FileFormat parameter entirely but the file that is saved is saved in text format even though it may have the .doc extension. In short, the formatting settings are lost. The WordBasic FileSaveAs also does that. So while there is progress there is still something that prevents the FileFormat parameter from being accepted.
Re: "type mismatch" in Word
by esr (Scribe) on Dec 03, 2004 at 01:07 UTC
    Thanks for all the comments but I could not get the SaveAs to work as long as I used the <mumble> constant name. When I finally replaced the "wdFormatDocument" with its value of "0" it worked just fine. BTFSPLK! This is on an XP Pro system using Office 2003 and ActivePerl -- if this is helpful to anyone else.
      Well, DUH! After I posted the last item I found the problem. I had included the line
      use Win32::OLE::Const 'Microsoft Excel';
      but I had a typo in the line
      use Win32::OLE::Const 'Microsoft Word';
      However the error message "text mismatch" was certainly not obvious to me as an indicator of the problem. :-(

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://411091]
Front-paged by diotalevi
and a kettle whistles...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2018-06-23 05:08 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.