如何能够在Delphi自动化中设置诸如legal、A4等页面--在CreateOleObject('Word.Application')之后,并在C驱动器中保存delphi创建的具有特定名称的Word文档。
发布于 2018-07-15 18:03:54
下面的代码将创建一个具有指定纸张大小的文档,并以指定的名称保存它:
uses ... Word2000;
procedure TForm1.CreateDocWithPaperSize;
var
MSWord,
Document,
PageSetUp: OleVariant;
AFileName : String;
iDocument : WordDocument;
begin
MsWord := CreateOleObject('Word.Application');
MsWord.Visible := True;
Document := MSWord.Documents.Add;
MSWord.Selection.Font.Size := 22;
MSWord.Selection.Font.Bold := true;
MSWord.Selection.TypeText(#13#10);
// the following is to get the WordDocument interface 'inside' the
// Document variant, so that we can use code completion on
// iDocument in the IDE to inspect its properties
iDocument := IDispatch(Document) as WordDocument;
PageSetUp := iDocument.PageSetup;
PageSetUp.PaperSize := wdPaperLegal;
MSWord.Selection.TypeText('Hello Word.');
AFileName := 'C:\Temp\Test.Docx';
Document.SaveAs(FileName := AFileName);
end;Word2000.Pas是import类型库的一个导入单元(还有其他版本--参见Delphi设置中OCX文件夹下的Servers子文件夹)。在里面,寻找
wdPaperSize
,您会发现它被声明为TOleEnum。在此下面,您将找到一个常量列表,这些常量允许您指定特定的纸张大小。
{ From Word2000.Pas }
// Constants for enum WdPaperSize
type
WdPaperSize = TOleEnum;
const
wdPaper10x14 = $00000000;
wdPaper11x17 = $00000001;
wdPaperLetter = $00000002;
wdPaperLetterSmall = $00000003;
wdPaperLegal = $00000004;
wdPaperExecutive = $00000005;
wdPaperA3 = $00000006;
wdPaperA4 = $00000007;
wdPaperA4Small = $00000008;
wdPaperA5 = $00000009;
wdPaperB4 = $0000000A;
wdPaperB5 = $0000000B;
wdPaperCSheet = $0000000C;
// etchttps://stackoverflow.com/questions/51350563
复制相似问题