2011-08-18 52 views
3
#!C:\Perl\bin\perl.exe 
use warnings; 
use 5.012; 
use Win32::OLE; 
use Win32::OLE::Const 'Microsoft Word'; 
$Win32::OLE::Warn = 3; 

my $word; 
eval{ $word = Win32::OLE->GetActiveObject('Word.Application'); }; 
die 'Word not Installed' if [email protected]; 
if (not defined $word) { 
    $word = Win32::OLE->new('Word.Application', 'Quit') or die 'Cannot start Word: ', Win32::OLE->LastError(); 
} 
die 'Word -> Documents is unavailable' if not $word -> Documents; 

my $file = 'testword'; 
my $doc = $word -> Documents -> Add() or die Win32::OLE->LastError();; 
my $select = $word -> Selection; 

my $table = $doc->Tables->Add({ Range => $select->{Range}, NumRows => 10, NumColumns => 2 }); 

$select->MoveRight({ Unit => wdCell, Count => 1 }); # $table->Cell({ Row => 0, Column => 1 }) 
$select->fields->Add({ Range => $select->Range, Type => wdFieldMacroButton, Text => 'NoName [Vorname]' }); 
$select->MoveDown({ Unit => wdLine, Count => 1 }); # $table->Cell({ Row => 1, Column => 1 }) 
$select->fields->Add({ Range => $select->Range, Type => wdFieldMacroButton, Text => 'NoName [Nachname]' }); 
$select->MoveDown({ Unit => wdLine, Count => 1 }); # $table->Cell({ Row => 2, Column => 1 }) 
$select->fields->Add({ Range => $select->Range, Type => wdFieldMacroButton, Text => 'NoName [Strasse Nr]' }); 

$doc -> SaveAs({ FileName => $file, FileFormat => wdFormatTemplate }); 
$doc -> Close(); 

我怎麼會在這裏的表指示cell-rowcell-column,而不是使用$select->MoveRight$select->MoveDown移動,...?Win32 :: OLE - Word:如何在表格中移動?

回答

-1

首先,獲取一個單元對象(例如:$cell = $table->Cell({ Row => 0, Column => 1 })),然後用$cell->Select()來選擇它。

參考:http://msdn.microsoft.com/en-us/library/bb241174%28v=office.12%29.aspx

+0

在我的情況下,單元沒有做任何事情,特別是在這種方式。這就是爲什麼我不嘗試將其他語言文檔自動轉換爲我的語言文檔的原因。什麼工作是使用範圍而不是單元格。 –

+0

我道歉,沒有看到它是關於Word的。 –

相關問題