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
  AShape  : Twdf2000Shape;
  i       : Integer;
  iOle    : OleVariant;
  strText : String;
begin
  for i := 1 to wdfWordDoc.Shapes.Count do begin
    iOle := i;
    AShape := wdfWordDoc.Shapes.Item(iOle);

    //type_がmsoPictureとワードアートだけを検出
    if (AShape.type_ = msoPicture) or (AShape.type_ = msoTextEffect) then begin
      AShape.Select(EmptyParam);
      strText := '番号: ' + IntToStr(i) + '   名前: ' + AShape.Name;
      MessageBox(Handle, PChar(StrText), '情報', MB_ICONINFORMATION);
    end;
  end;
end;




選択中の図の番号と名前を取得


選択中の図の番号と名前を取得して表示します.


リスト2
procedure TplWordFormEx2.Button2Click(Sender: TObject);
var
  AShape  : Twdf2000Shape;
  i       : Integer;
  iOle    : OleVariant;
  strText : String;
begin
  //Shapeオブジェクトがない場合は中止
  if wdfWordDoc.Shapes.Count < 1 then exit;

  //選択中のShapeオブジェクトを検索していく
  //インラインShapeが選択中だとエラー
  try
    for i := 1 to wdfWordApp.Selection.ShapeRange.Count do begin
      iOle   := i;
      AShape := wdfWordApp.Selection.ShapeRange.Item(iOle);

      //type_がmsoPictureとワードアートだけを検出
      if (AShape.type_ = msoPicture) or (AShape.type_ = msoTextEffect) then begin
        strText := '番号: ' + IntToStr(i) + '   名前: ' + AShape.Name;
        MessageBox(Handle, PChar(StrText), '情報', MB_ICONINFORMATION);
      end;
    end;
  except
  end;
end;




文書内のインラインシェイプの番号を取得


同じく文書中の図の番号だけを取得するサンプルです.
ただし,以下のコードはインラインシェイプ (InlineShape) の場合です.InlineShape は,位置を固定した図,画像のことです.マウスで掴んで文書内の他の場所に移動できません.


リスト3
procedure TplWordFormEx2.Button3Click(Sender: TObject);
var
  AShape  : Word2000.InlineShape;
  i       : Integer;
  strText : String;
begin
  for i := 1 to wdfWordDoc.InlineShapes.Count do begin
    AShape := wdfWordDoc.InlineShapes.Item(i);
    AShape.Select;
    strText := '番号: ' + IntToStr(i) + '   タイプ: ' + IntToStr(AShape.type_);
    MessageBox(Handle, PChar(StrText), '情報', MB_ICONINFORMATION);
  end;
end;




選択中のインラインシェイプの番号を取得


同じくインラインシェイプですが,選択中の図の番号を取得して表示します.


リスト4
procedure TplWordFormEx2.Button4Click(Sender: TObject);
var
  AShape  : Word2000.InlineShape;
  i       : Integer;
  strText : String;
begin
  //選択中のインラインシェイプを検索
  for i := 1 to wdfWordApp.Selection.InlineShapes.Count do begin
    AShape  := wdfWordApp.Selection.InlineShapes.Item(i);
    strText := '番号: ' + IntToStr(i) + '   タイプ: ' + IntToStr(AShape.type_);
    MessageBox(Handle, PChar(StrText), '情報', MB_ICONINFORMATION);
  end;
end;




文書内のテキストボックスの番号と名前を取得


テキストボックスの番号と名前も作成順に付けられます.変更出来ません.開発時にはこの番号または名前を知らないと操作ができません.
次のコードは,そのテキストボックスの番号と名前を取得してテキストボックス内に描画します.このコードはグループ化したテキストボックスには利用できません.


リスト5
procedure TplWordFormEx2.Button1Click(Sender: TObject);
var
  AShape : Twdf2000Shape;
  i      : Integer;
  iOle   : OleVariant;
begin
  for i := 1 to wdfWordDoc.Shapes.Count do begin
    iOle := i;
    AShape := wdfWordDoc.Shapes.Item(iOle);
    AShape.Select(EmptyParam);
    try
      AShape.TextFrame.TextRange.Font.Size := 20;
      AShape.TextFrame.TextRange.Text      := VarToStr(i) + sLineBreak + AShape.Name;
    except
    end;
  end;
end;




選択中のテキストボックスの番号と名前を取得


同じくテキストボックスのサンプルですが,選択中のテキストボックスだけの番号と名前を取得して,それをそのテキストボックスに表示します.
上のサンプルと同じく,グループ化したテキストボックスには利用できません.


リスト6
procedure TplWordFormEx2.Button2Click(Sender: TObject);
var
  AShape  : Twdf2000Shape;
  i       : Integer;
  iOle    : OleVariant;
  strText : String;
begin
  //Shapeオブジェクトがない場合は中止
  if wdfWordDoc.Shapes.Count < 1 then exit;

  //選択中のShapeオブジェクトを検索していく
  //インラインShapeが選択中だとエラー
  try
    for i := 1 to wdfWordApp.Selection.ShapeRange.Count do begin
      iOle   := i;
      AShape := wdfWordApp.Selection.ShapeRange.Item(iOle);

      //type_がmsoTextBoxだけを検出
      if (AShape.type_ = msoTextBox) then begin
        strText := '番号: ' + IntToStr(i) + '   名前: ' + AShape.Name;
        AShape.TextFrame.TextRange.Font.Size := 20;
        AShape.TextFrame.TextRange.Text      := VarToStr(i) + sLineBreak + AShape.Name;
      end;
    end;
  except
  end;
end;




文書内の表の番号を取得


表の番号は作成順に付けられます.変更出来ません.開発時にはこの番号を知らないと表に対する操作ができません.
次のコードは,文書内の表の表番号を取得して,その表のセルにその番号を表示します.


リスト7
procedure TplWordFormEx2.Button1Click(Sender: TObject);
var
  pTable  : OleVariant;
  pRow    : OleVariant;
  pCol    : OleVariant;
  i       : Integer;
  StrText : String;
begin
  pRow := 1;;
  pCol := 1;

  //表の番号も行と列の数値も1から始まることに注意
  for i := 1 to wdfWordDoc.Tables.Count do begin
    pTable  := i;
    StrText := IntToStr(i) + '番目の表';
    wdfWordDoc.Tables.Item(pTable).Cell(pRow,pCol).Range.Text := StrText;
  end;
end;




インラインをシェイプに変換


Shape オブジェクトの ConvertToShape メソッドを使用すると,以下のように InlineShape を Shape に変換できます.
逆の変換は ConvertToInlineShape を使用します.


リスト8
procedure TplWordFormEx2.Button5Click(Sender: TObject);
var
  AShape : Word2000.InlineShape;
  i      : Integer;
begin
  try
    for i := 1 to wdfWordDoc.InlineShapes.Count do begin
      AShape := wdfWordDoc.InlineShapes.Item(i);
      AShape.ConvertToShape;
    end;
  except
  end;
end;