Windows Server で実行されている WinSSHD への ssh を使用した Windows 2008 R2 / perl 5.14.1 x64 /Word 2010 管理者アカウントへのリモート アクセス。この perl スクリプトは、(ローカル、管理者) コマンド ラインから正常に実行されます。
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();`
リモート シェルの出力は次のようになります。
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
ヒントはありますか?この問題は、リモートで実行するように設定する必要がある単語 {visible}=0 プロパティの使用に関連しているようです。私が見つけた 1 つの投稿では、完全な Microsoft Office 呪文を使用してアプリケーション オブジェクトという単語を作成することが提案されていましたが、これを win32::OLE が受け入れるもの、つまり Microsoft.Office.Interop.Word.ApplicationClass createobject ( "Word.Application") (特定の提案を見つけることはできませんでしたが、perlish ではありませんでした)。
助けてくれてありがとう