1

I'm trying to open a .docx file using the following code:

require Win32::OLE;
my $docfile = "C:/Users/me/Documents/file.docx";
my $Word = Win32::OLE->GetActiveObject('Word.Application');
unless ($Word) { $Word = Win32::OLE->new('Word.Application', sub {$_[0]->Quit;}) or die "oops\n"; }
$Word->{visible} = 1;
my $File = $Word->Documents->Open($docfile);
$File->PrintOut();
$File->Close(); 
$Word->Quit();

But i get the following error:

OLE exception from "Microsoft Word":

Sorry, we couldn’t find your file. Is it possible it was moved, renamed or deleted? (C://Users/me/Documents/...)

How can i fix this? Why does it add // to my path? (needless to say, the file does exist in the system and that is the correct path).

Thanks!

gambit20088
  • 321
  • 2
  • 12
  • 1
    I've never worked with Win32::OLE so this is a guess. Maybe it wants backslashes in the path? – simbabque Nov 17 '16 at 14:47
  • 1
    But be aware - you might need to escape them if you're double quoting. – Sobrique Nov 17 '16 at 14:48
  • 3
    Which version of Word is this? I do not have the most recent version, but maybe it's trying to treat it as a UNC or something. In any case, what happens if you just use `my $docfile = join '\\', qw(C: Users me Documents file.docx);`? Also, `use Win32::OLE; $Win32::OLE::Warn = 3;` – Sinan Ünür Nov 17 '16 at 14:48
  • Not everything on Windows will accept a `/` as a directory separator. You might have to write that as `my $docfile = 'C:\\Users\\me\\Documents\\file.docx';` The double slashes are necessary because ``\`` is the escape character. There's also the obvious question... are you *really sure* `C:\Users\me\Documents\file.docx` exists and is readable? Check it in the code with a `-r $docfile`. – Schwern Nov 17 '16 at 16:03
  • @Schwern Yes i'm sure, and unfortunately it didn't work. – gambit20088 Nov 17 '16 at 17:17

1 Answers1

1

I suggest that you use canonpath from File::Spec::Function to normalise the file path and change the path separators to backslashes

Like this

use strict;
use warnings 'all';

use Win32::OLE;
use File::Spec::Functions 'canonpath';

my $docfile = "C:/Users/me/Documents/file.docx";
my $word    = Win32::OLE->GetActiveObject('Word.Application') or die;

$word->{visible} = 1;

my $file = $word->Documents->Open(canonpath($docfile)) or die;
$file->PrintOut;
$file->Close; 

$word->Quit;
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • With this solution, how can i print out to the console the contents of the file? (as text) – gambit20088 Nov 17 '16 at 17:18
  • You can only do with `Win32::OLE` things that you could do manually with the Word application. You can't display plain text from Word, so you can't do it this way either. – Borodin Nov 18 '16 at 01:39
  • Ok.. so how could i do it then? Should i create a new post? – gambit20088 Nov 18 '16 at 02:11
  • You could use Perl and `Win32::OLE` to **Save as...** your document to a text file, and then read and print that file in the same program. I've closed your question as being a duplicate of another that has an alternative solution. If you need more help then, yes, ask another question. – Borodin Nov 18 '16 at 02:32