简体   繁体   English

通过 Delphi 在 word 中创建页码 X of Y

[英]Create Page number X of Y in word via Delphi

I want to add a page X of Y numbering in a word file generated via Delphi which is: - not bold;我想在通过 Delphi 生成的 word 文件中添加 Y 页编号的 X 页:- 不粗体; - in font size 8; - 字体大小为 8; - aligned to the right. - 右对齐。

Note that: Y is the total number of pages;注意:Y为总页数; X is the index of the page number. X 是页码的索引。

So far I've found this:到目前为止,我发现了这个:

procedure Print;
var v:olevariant;

v:=CreateOleObject('Word.Application');
v.Documents.Add;
HeaderandFooter;
firstpage:=true;  

procedure HeaderandFooter;
var adoc,:olevariant;
begin
adoc.Sections.Item(1).Headers.Item(wdHeaderFooterPrimary).Range.Font.Size := 8;
adoc.Sections.Item(1).Footers.Item(wdHeaderFooterPrimary).PageNumbers.Add(wdAlignPageNumberRight);

I can change the format of the numbering:我可以更改编号的格式:
adoc.Sections.Item(1).Footers.Item(wdHeaderFooterPrimary).PageNumbers.NumberStyle:= wdPageNumberStyleLowercaseRoman; adoc.Sections.Item(1).Footers.Item(wdHeaderFooterPrimary).PageNumbers.NumberStyle:= wdPageNumberStyleLowercaseRoman;

But there is no option for page X of Y format.但是 Y 格式的页面 X 没有选项。 How do I implement that?我该如何实施?

Although there is not a selectable header page-numbering style which has this format, you can do this by adding specific MS Word Document Fields, the PAGE and NUMPAGES ones, to the page header (or footer), or elsewhere. 虽然没有可选择的标题页编号样式具有此格式,但您可以通过将特定的MS Word文档字段,PAGE和NUMPAGES字段添加到页眉(或页脚)或其他位置来完成此操作。

procedure TForm1.MakeDocWithPageNumbers;
var
  MSWord,
  Document : OleVariant;
  AFileName,
  DocText : String;
begin
  MSWord := CreateOleObject('Word.Application');
  MSWord.Visible := True;

  Document := MSWord.Documents.Add;
  DocText := 'Hello Word!';
  MSWord.Selection.TypeText(DocText);

  if MSWord.ActiveWindow.View.SplitSpecial <> wdPaneNone then
      MSWord.ActiveWindow.Panes(2).Close;
  if (MSWord.ActiveWindow.ActivePane.View.Type = wdNormalView) or (MSWord.ActiveWindow.ActivePane.View.Type = wdOutlineView) then
      MSWord.ActiveWindow.ActivePane.View.Type := wdPrintView;

  MSWord.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader;
  MSWord.Selection.TypeText( Text:='Page ');

  MSWord.Selection.Fields.Add( Range:= MSWord.Selection.Range, Type:=wdFieldEmpty, 
    Text:= 'PAGE  \* Arabic ', PreserveFormatting:=True);
  MSWord.Selection.TypeText( Text:=' of ');
  MSWord.Selection.Fields.Add( Range:=MSWord.Selection.Range, Type:=wdFieldEmpty,
    Text:= 'NUMPAGES  \* Arabic ', PreserveFormatting:=True);
  MSWord.Selection.GoTo(What:=wdGoToPage, Which:=wdGoToNext, Count:=1);

  AFileName := 'd:\aaad7\officeauto\worddocwithheader.docx';
  Document.SaveAs(AFileName);
  ShowMessage('Paused');
  Document.Close;
end;

I've left setting the font size and right-alignment as exercises for the reader as SO isn't supposed to be a code-writing service ;=) 我已经将字体大小和右对齐设置为读者练习,因为SO不应该是代码编写服务; =)

I found your code very useful, Thanks MartynA.我发现您的代码非常有用,谢谢 MartynA。 Here is my version (a bit simplified) >>这是我的版本(有点简化)>>

  WordApp := CreateOleObject('Word.Application'); // Create a Word Instance
  WordApp.Visible := True; 
  MyDoc := WordApp.Documents.Add;  // Add a new Doc
  MyDoc.ActiveWindow.ActivePane.View.SeekView := 10; // Footer Selected
  WordApp.Selection.TypeText('Page: ');  // Add text to footer
  WordApp.Selection.Fields.Add(WordApp.Selection.Range, 33); // Add Page Counter field to footer
  WordApp.Selection.TypeText(' of ');  // Add text to footer
  WordApp.Selection.Fields.Add(WordApp.Selection.Range, 26); // Add Page Number field to footer
  MyDoc.ActiveWindow.ActivePane.View.SeekView := 0; // Main Doc Selected
  ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM