如何在 Delphi Word 自动化中设置合法、A4 等页面 - 在 CreateOleObject('Word.Application') 之后,并将 Delphi 创建的 Word 文档以特定名称保存在 C 盘中。
最佳答案
下面的代码将创建一个具有指定纸张尺寸的文档并将其保存在指定名称下:
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
是 MS Word 类型库的导入单元(还有其他版本 - 请参阅 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;
// etc
关于Delphi Ms word自动化页面在Windows 10中设置和保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51350563/