【问题标题】:Delphi Ms word Automation Page setup and save in windows 10Delphi Ms word自动化页面设置并保存在windows 10中
【发布时间】:2018-07-16 02:03:54
【问题描述】:

如何在 Delphi Word 自动化中设置法律、A4 等页面 - 在 CreateOleObject('Word.Application') 之后,并将 delphi 创建的 Word 文档以特定名称保存在 C 驱动器中。

【问题讨论】:

  • Delphi 和 MS Word 的哪个版本?
  • Delphi Xe10.2 和 Ms office 2016

标签: delphi ms-word


【解决方案1】:

下面的代码将创建一个具有指定纸张大小的文档并以指定的名称保存:

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

【讨论】:

  • 谢谢。我使用的是 Windows 10。保存到 C 盘时显示错误 - 写保护。但是我可以保存在D盘或E盘:,怎么可能保存在windows 10-,C盘
  • 您的 C: 盘究竟在哪里?阅读有关写访问权限的信息。顺便说一句,这与您在 q 中提出的问题完全不同。
  • 我的意思是 'C:\Temp\Test.Docx';在 Windows 10 中是不可能的
  • 在资源管理器中右键单击 C:\Temp,选择属性并查看安全选项卡。这将告诉你谁对文件夹有写权限,谁没有写权限。
  • delphi ms-word 自动化的最佳参考手册或链接是什么
猜你喜欢
  • 2012-09-19
  • 2022-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多