使用ssh到Windows Server上运行的WinSSHD远程访问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();`
远程shell输出如下所示:
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属性有关,该属性必须设置为远程运行。我发现一篇帖子建议使用完整的Microsoft Office incantion来创建单词应用程序对象,但我无法弄清楚如何将其转换为win32 :: OLE将接受的内容,即 Microsoft.Office.Interop.Word.ApplicationClass createobject(“Word.Application”) (我无法找到具体的建议,但不是很有意义。)
感谢您的帮助
答案 0 :(得分:1)
有一点是FileName => 'exampletext.doc'
- 这不是绝对的路径......也许它试图在不允许的地方保存或者没有足够的空间......
这可能与这一点有关:
WinSSHD是作为服务运行的吗?
如果是,则根据MS不支持办公自动化 - 请参阅http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2