1

Delphi 経由で生成されたワード ファイルにページ 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(wdHeaderFooterPrimary).PageNumbers.NumberStyle := wdPageNumberStyleLowercaseRoman;

ただし、ページ X of Y 形式のオプションはありません。どうすればそれを実装できますか?

4

1 に答える 1

3

この形式の選択可能なヘッダー ページ番号スタイルはありませんが、特定の 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;

SOはコード作成サービスではないはずなので、読者の演習としてフォントサイズと右揃えの設定を残しました;=)

于 2016-01-07T14:03:06.803 に答える