Delphi Ms word自动化页面在Windows 10中设置和保存

标签 delphi ms-word

如何在 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/

相关文章:

delphi - 使所有者绘制的 TPageControl 选项卡看起来更好,就像没有所有者绘制一样

c# - 从 Delphi 应用程序中使用 SerialPort 类调用 C# 非托管导出 DLL

class - 为什么这段代码有效,Delphi 在这种情况下如何实例化一个类?

javascript - Response.Redirect Word 文档故障?

ms-word - MS Word : change existing captions name

c# - 什么属性定义段落或表格的边距?

xml - 通过 VBA 和剪贴板将 HTML 复制到 Word 会丢失特殊字符

mysql - Delphi 中“where 子句中的未知列”,但 MySQL 中没有

delphi - HasValidFileNameChars 对于 UNC 文件失败

documentation - 有没有办法同步不同 Word 文档的部分?