通过Delphi在word中创建第X页,共Y页

通过Delphi在word中创建第X页,共Y页,delphi,ms-word,page-numbering,Delphi,Ms Word,Page Numbering,我想在通过Delphi生成的word文件中添加一个X页,共Y页编号,即: -不大胆; -字体大小为8; -向右对齐 请注意: Y是总页数; X是页码的索引 到目前为止,我发现: procedure Print; var v:olevariant; v:=CreateOleObject('Word.Application'); v.Documents.Add; HeaderandFooter; firstpage:=true; procedure HeaderandFooter; var

我想在通过Delphi生成的word文件中添加一个X页,共Y页编号,即: -不大胆; -字体大小为8; -向右对齐

请注意: Y是总页数; X是页码的索引

到目前为止,我发现:

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);
我可以更改编号的格式:
adoc.Sections.Item(1).Footers.Item(WDHeaderFooPerprimary).PageNumbers.NumberStyle:=wdPageNumberStyleLowercaseRoman


但对于Y格式的第X页,没有选项。如何实现这一点?

尽管没有具有此格式的可选页眉页码编号样式,但您可以通过向页眉(或页脚)或其他位置添加特定的MS Word文档字段(页面和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;
程序TForm1.makedocwithpagenumber;
变量
MSWord,
文件:Olevant;
阿菲尔名字,
DocText:字符串;
开始
MSWord:=CreateOleObject('Word.Application');
MSWord.Visible:=真;
单据:=MSWord.Documents.Add;
DocText:=“你好!”;
MSWord.Selection.TypeText(DocText);
如果MSWord.ActiveWindow.View.SplitSpecial WDPaneOne,则
MSWord.ActiveWindow.Panes(2)关闭;
如果(MSWord.ActiveWindow.ActivePane.View.Type=wdNormalView)或(MSWord.ActiveWindow.ActivePane.View.Type=wdOutlineView),则
MSWord.ActiveWindow.ActivePane.View.Type:=wdPrintView;
MSWord.ActiveWindow.ActivePane.View.SeekView:=wdSeekCurrentPageHeader;
MSWord.Selection.TypeText(文本:='Page');
MSWord.Selection.Fields.Add(范围:=MSWord.Selection.Range,类型:=wdFieldEmpty,
文本:='PAGE\*阿拉伯语',保留格式:=True);
MSWord.Selection.TypeText(文本:='of');
MSWord.Selection.Fields.Add(范围:=MSWord.Selection.Range,类型:=wdFieldEmpty,
文本:='NUMPAGES\*阿拉伯语',保留格式:=True);
MSWord.Selection.GoTo(What:=wdGoToPage,Which:=wdGoToNext,Count:=1);
AFileName:=“d:\aaad7\officeauto\worddocwithheader.docx”;
Document.SaveAs(文件名);
ShowMessage(‘暂停’);
文件。关闭;
结束;

我把设置字体大小和右对齐作为读者的练习,因为这样不应该是代码编写服务;=)

工作起来很有魅力!我通过:
MSWord.Selection.ParagraphFormat.Alignment:=wdAlignParagraphRight将其右对齐