1
votes

Remote access to Windows 2008 R2 / perl 5.14.1 x64 /Word 2010 administrator account using ssh to WinSSHD running on the Windows Server. This perl script runs fine from the (local, Admin) command line:

use warnings;
use strict;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft.Word';    # wd  constants
use Win32::OLE::Const 'Microsoft Office 14.0 Object Library';  # mso constants
use Win32::OLE qw( in with );
my $word = CreateObject Win32::OLE 'Word.Application' or die $!;
$word->{'Visible'} = 0; # note, for debugging only; otherwise use 0
print ">> Creating a new document\n";
my $document = $word->Documents->Add;
with(   $document->{BuiltinDocumentProperties},
Title => "OLE Word Perl",
Author => "Car Friedberg",
Subject => "simple example" );
print ">> Creating a selection at insertion point\n";
# selection is the insertion point.
my $selection = $word->Selection;

print ">> Insert text \n";
$selection->TypeText("This is a test.");
$selection->TypeParagraph;
$selection->TypeText( "End of test.");

print ">> Save document \n";
# save the document (works with Word 2010) (could use wdFormatPDF or wdFormatRTF)
$word->ActiveDocument->SaveAs({
FileName => 'exampletext.doc',
FileFormat =>  wdFormatDocument,
LockComments => msoFalse,
Password => "", 
AddToRecentFiles =>  msoFalse,
    WritePassword => "", 
    ReadOnlyRecommended => msoFalse, 
    EmbedTrueTypeFonts =>  msoFalse, 
    SaveNativePictureFormat => msoFalse, 
    SaveFormsData => msoFalse,
    SaveAsAOCELetter => msoFalse});

$word->ActiveDocument->Close(wdDoNotSaveChanges);
$word->Quit();`

The remote shell output looks like this:

C:\Users\Administrator>perl -w \winbat\ole_example.pl
>> Creating a new document
>> Creating a selection at insertion point
>> Insert text >> Save document
OLE exception from "Microsoft Word":Command failedWin32::OLE(0.1709) error 0x800a1066
in METHOD/PROPERTYGET "SaveAs" at \winbat\ole_example.pl line 34

Any hints? I gather that the problem is related to using the word {visible}=0 property, which must be set to run remotely. One post I found suggested using the full Microsoft Office incantion to create the word application object, but I could not figure out how to translate this to something that win32::OLE would accept, i.e. Microsoft.Office.Interop.Word.ApplicationClass createobject ("Word.Application") (I have not been able to locate the specific suggestion, but it was not perlish).

Thanks for any help

1

1 Answers

1
votes

One point is FileName => 'exampletext.doc' - this is not an absolute path... perhaps it is trying to save somewhere it is not allowed or not enough space...

Which could be related to this point:
Is WinSSHD running as a service ?
IF yes, then automation of office is NOT supported according to MS - see http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2