オートメーションを使用して Excel を起動したときに、Excel UDF クラスを Excel で使用できるようにするのに問題があります。私が作成しているアドインは、サード パーティのアプリケーションから Excel を起動したときに機能する必要があります。
これが仕様によるものであることは理解していますが、自動化によって Excel が開かれたときにユーザーが UDF を使用できるようにする方法が必要です。
Excel 2007 用の VSTO アドインと UDF クラスを作成しました。コードをコピーし、この Web ページの指示に従って、GUID を指示どおりに置き換えました。 http://csharpramblings.blogspot.ca/2011/09/communicating-between-vsto-and-udfs-in.html
Excel を開くと、新しいアドインが表示され、関数のリストに UDF が使用可能として表示されます (関数の挿入)。だから私はそれが働いていることを知っています。
次に、Excel オートメーションを使用して Excel を開くアプリケーションを作成しました。
これは Windows フォーム アプリケーションで、Form1 にボタンを配置します。(私のマシンでは) C:\Windows\assembly\GAC\Microsoft.Office.Interop.Excel\12.0.0.0__71e9bce111e9429c\Microsoft に解決される Microsoft.Office.Interop.Excel (バージョン 1.6.0.0) への参照を追加しました。 Office.Interop.Excel.dll
http://support.microsoft.com/kb/302084から次のコードを取得しましたが、データをワークシートのセルに入れるコードの一部は不要だったので削除しました。Excel を開く例が欲しかっただけです。自動化を使用しています。
サンプル プログラムの Form1 の C# コード:
using System;
using System.Reflection;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelAutomationTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;
try
{
//Start Excel and get Application object.
oXL = new Excel.Application();
oXL.Visible = true;
//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
//Add table headers going cell by cell.
oSheet.Cells[1, 1] = "First Name";
oSheet.Cells[1, 2] = "Last Name";
oSheet.Cells[1, 3] = "Full Name";
oSheet.Cells[1, 4] = "Salary";
//Make sure Excel is visible and give the user control
//of Microsoft Excel's lifetime.
oXL.Visible = true;
oXL.UserControl = true;
}
catch (Exception theException)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, theException.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, theException.Source);
MessageBox.Show(errorMessage, "Error");
}
}
}
Excel を自動化する Windows フォーム アプリをビルドします。アプリを実行します。エクセルが起動します。[数式] タブに移動し、[関数の挿入] をクリックします。「すべて」の機能を表示します。私のマシンでは、MYINT はリストにありません。
オートメーションを使用して Excel を開いたときに MYINT 関数を使用できるようにすることはできますか? ある人は、VSTO ThisAddIn_Startup() で Excel.Application オブジェクトを使用して UDF を読み込むことができると提案しましたが、その方法のサンプルを見つけることができず、私を助けてくれた人も知りませんでした。
MSDN の Excel および VSTO フォーラムで支援を求めましたが、次に何をすべきか明確なイメージを得ることができませんでした。同様の質問がいくつかありますが、StackOverflow に関してまったく同じものはありません。誰にもアイデアはありますか?