Delphi Programming / Object Pascal
サイトのトップページへ リンクのページへ ロゴマーク
[掲載 2009年05月12日] [更新 2013年11月28日] ワード操作フォームEx
   
ワード操作フォームEx   参考コード類
現在の頁番号と総頁数
動作確認等 Windows 7 U64(SP1) + Delphi XE2 + Word 2010







現在の頁番号の取得


入力キャレット(カーソル)がある頁の通し頁番号を取得します.


リスト1 
procedure TplWordFormEx2.Button1Click(Sender: TObject);
var
  PageNo     :  Integer;
  PageSerial : Integer;
  strPage    : String;
begin
  //何頁目かを取得
  PageSerial := wdfWordApp.Selection.Information[wdActiveEndPageNumber];
  //頁番号を取得.1頁目が5なら2頁目は6となる
  PageNo  := wdfWordApp.Selection.Information[wdActiveEndAdjustedPageNumber];

  strPage := '現在 ' + IntToStr(PageSerial) + ' 頁目で,'+
             '頁番号は ' + IntToStr(PageNo )+ ' です.';
  MessageBox(Handle, PChar(StrPage), '情報', MB_ICONINFORMATION);
end;




頁数の取得


現在の文書の頁番号を取得するサンプルコードです.


リスト2 
//=============================================================================
//  全頁数の取得 1
//=============================================================================
procedure TplWordFormEx2.Button2Click(Sender: TObject);
var
  objWordDoc : OleVariant;
  Pages      : Integer;
  strPage    : String;
begin
  objWordDoc := wdfWordApp.ActiveDocument;
  Pages      := objWordDoc.BuiltInDocumentProperties[wdPropertyPages].Value;

  strPage := '全部で ' + IntToStr(Pages) + ' 頁あります.';
  MessageBox(Handle, PChar(StrPage), '情報', MB_ICONINFORMATION);
end;

//=============================================================================
//  全頁数の取得 2
//=============================================================================
procedure TplWordFormEx2.Button3Click(Sender: TObject);
var
  Pages   : Integer;
  strPage : String;
begin
  Pages := wdfWordApp.Selection.Information[wdNumberOfPagesInDocument];

  strPage := '全部で ' + IntToStr(Pages) + ' 頁あります.';
  MessageBox(Handle, PChar(StrPage), '情報', MB_ICONINFORMATION);
end;




ページ番号設定ダイアログの表示


文書にページ番号を挿入するのはコードでもできますが,ダイアログで設定する際の参考です.
このダイアログの [書式] ボタンで,この後のページ番号の書式ダイアログを表示できます.


図1
ページ番号設定ダイアログ


リスト3 
procedure TplWordFormEx2.Button4Click(Sender: TObject);
var
  objDialog : OleVariant;
begin
  wdfSetFocusWord;
  try
    objDialog := wdfWordApp.Dialogs.Item(wdDialogInsertPageNumbers);
    objDialog.Show;
  except
    raise;
  end;
  wdfSetFocusWord;
end;




ページ番号の書式ダイアログの表示


ページ番号の書式もコードでもできますが,ダイアログで設定する際の参考です.


図2
ページ番号の書式設定用のダイアログ


リスト4 
procedure TplWordFormEx2.Button5Click(Sender: TObject);
var
  objDialog : OleVariant;
begin
  wdfSetFocusWord;
  try
    objDialog := wdfWordApp.Dialogs.Item(wdDialogFormatPageNumber);
    objDialog.Show;
  except
    raise;
  end;
  wdfSetFocusWord;
end;