オートシェイプ
オートシェイプの作成(Excel編)
オートシェイプの作成は、Excel.Shapesクラスの各メソッドを使用して行います。ここでは、代表的な3つのメソッドについて、具体例を示します。
AddShapeメソッド
Excelブックプロジェクトを選択し、ドキュメントを新規に作成します。
Sheet1.csのコードを開き、イベントハンドラのひな型を実装し、BeforeDoubleClickイベントに登録します。
private void Sheet1_Startup(object sender, System.EventArgs e) { this.BeforeDoubleClick += new Excel.DocEvents_BeforeDoubleClickEventHandler( Sheet1_BeforeDoubleClick); } private void Sheet1_Shutdown(object sender, System.EventArgs e) { } private void Sheet1_BeforeDoubleClick(Excel.Range Target, ref bool Cancel) { }
イベントハンドラの実装を行います。ここでは、フローチャートを生成し*1、テキストを追加しています。
private void Sheet1_BeforeDoubleClick(Excel.Range Target, ref bool Cancel) { // オートシェイプを作成 Excel.Shape shape = ((Excel.Worksheet)this.Application.ActiveSheet). Shapes.AddShape(Office.MsoAutoShapeType.msoShapeFlowchartProcess, 0f, 0f, 100f, 100f); // オートシェイプにテキストを追加 shape.TextFrame.Characters(0, 0).Insert("ほげほげ"); // オートシェイプのテキストの一部の色を変える shape.TextFrame.Characters(0, 2).Font.Color = (object)0x00ff00; }
AddLineメソッド
前節で作成したソリューションを使います。
イベントハンドラに、直線を引く処理を追加します。
private void Sheet1_BeforeDoubleClick(Excel.Range Target, ref bool Cancel) { // オートシェイプを作成 Excel.Shape shape = ((Excel.Worksheet)this.Application.ActiveSheet).Shapes.AddShape( Office.MsoAutoShapeType.msoShapeFlowchartProcess, 0f, 0f, 100f, 100f); // オートシェイプにテキストを追加 shape.TextFrame.Characters(0, 0).Insert("ほげほげ"); // オートシェイプのテキストの一部の色を変える shape.TextFrame.Characters(0, 2).Font.Color = (object)0x00ff00; // 直線を作成 Excel.Shape shape2 = ((Excel.Worksheet)this.Application.ActiveSheet).Shapes.AddLine( 200f, 0f, 200f, 100f); }
実行し、Sheet1上でダブルクリックを行うと、以下のように、右側に直線が追加されます。
次に、直線を矢印に変更するため*2、以下を追加します。
private void Sheet1_BeforeDoubleClick(Excel.Range Target, ref bool Cancel) { // オートシェイプを作成 Excel.Shape shape = ((Excel.Worksheet)this.Application.ActiveSheet).Shapes.AddShape( Office.MsoAutoShapeType.msoShapeFlowchartProcess, 0f, 0f, 100f, 100f); // オートシェイプにテキストを追加 shape.TextFrame.Characters(0, 0).Insert("ほげほげ"); // オートシェイプのテキストの一部の色を変える shape.TextFrame.Characters(0, 2).Font.Color = (object)0x00ff00; // 直線を作成 Excel.Shape shape2 = ((Excel.Worksheet)this.Application.ActiveSheet).Shapes.AddLine( 200f, 0f, 200f, 100f); // 直線を矢印に変更 shape2.Line.EndArrowheadStyle = Office.MsoArrowheadStyle.msoArrowheadTriangle; }
AddConnectorメソッド
前節で作成したソリューションを使います。
イベントハンドラに、コネクタを生成する*3コードを追加します。
private void Sheet1_BeforeDoubleClick(Excel.Range Target, ref bool Cancel) { // オートシェイプを作成 Excel.Shape shape = ((Excel.Worksheet)this.Application.ActiveSheet).Shapes.AddShape( Office.MsoAutoShapeType.msoShapeFlowchartProcess, 0f, 0f, 100f, 100f); // オートシェイプにテキストを追加 shape.TextFrame.Characters(0, 0).Insert("ほげほげ"); // オートシェイプのテキストの一部の色を変える shape.TextFrame.Characters(0, 2).Font.Color = (object)0x00ff00; // 直線を作成 Excel.Shape shape2 = ((Excel.Worksheet)this.Application.ActiveSheet).Shapes.AddLine( 200f, 0f, 200f, 100f); // 直線を矢印に変更 shape2.Line.EndArrowheadStyle = Microsoft.Office.Core.MsoArrowheadStyle.msoArrowheadTriangle; // コネクタを作成 Excel.Shape shape3 = ((Excel.Worksheet)this.Application.ActiveSheet).Shapes.AddConnector( Office.MsoConnectorType.msoConnectorCurve, 100f, 0f, 200f, 100f); }
実行し、Sheet1上でダブルクリックを行うと、以下のように表示されます。
次に、コネクタを接続するため、以下のコードを追加します。
private void Sheet1_BeforeDoubleClick(Excel.Range Target, ref bool Cancel) { // オートシェイプを作成 Excel.Shape shape = ((Excel.Worksheet)this.Application.ActiveSheet).Shapes.AddShape( Office.MsoAutoShapeType.msoShapeFlowchartProcess, 0f, 0f, 100f, 100f); // オートシェイプにテキストを追加 shape.TextFrame.Characters(0, 0).Insert("ほげほげ"); // オートシェイプのテキストの一部の色を変える shape.TextFrame.Characters(0, 2).Font.Color = (object)0x00ff00; // 直線を作成 Excel.Shape shape2 = ((Excel.Worksheet)this.Application.ActiveSheet).Shapes.AddLine( 200f, 0f, 200f, 100f); // 直線を矢印に変更 shape2.Line.EndArrowheadStyle = Office.MsoArrowheadStyle.msoArrowheadTriangle; // コネクタを作成 Excel.Shape shape3 = ((Excel.Worksheet)this.Application.ActiveSheet).Shapes.AddConnector( Office.MsoConnectorType.msoConnectorCurve, 100f, 0f, 200f, 100f); // コネクタを接続する shape3.ConnectorFormat.BeginConnect(shape, 4); shape3.ConnectorFormat.EndConnect(shape2, 1); }
実行し、Sheet1上でダブルクリックを行うと、以下のように表示されます。
矢印をドラッグすると、接続されたコネクタが追随してきます。
オートシェイプの作成(Word編)
基本的にはExcelと同じなのですが、各メソッドに、Anchorという省略可能な引数が増えており、オートシェイプを関連付ける文字列を表す Range オブジェクトを指定できるようになっています。