2012-01-03 9 views
3

Excelのオートメーションアドインを作成しました。これは、UDFラッパーを含むac#クラスライブラリとして実装されています(VSTOは使用しません)。 UDFは次のようになります。UDFからExcelセルのアドレスを取得

string foo(string data){ 

    //Do some work on the data passed 
    string result; 
    return(result); 
} 

追加パラメータを渡すことなく、この数式が入力されたセルのアドレスを暗黙的に取得する方法はありますか? 1つの方法は、アドインがロードされるとすぐにイベントリスナーをブックにフックし、セルの値が変化したときにイベントをキャプチャすることです。しかし、私はそれに代わるものを探しています。

おかげで、

+0

私はあなたが何をしているかについては明らかではないですが、しかし、VBAでApplication.Callerは、それが中だ –

答えて

4

あなたがGlobals.ThisAddIn.Application.Callerを試すことができ、それは、細胞を含むExcel.Rangeを返します。たぶん正しい方向に私を指しているためbrijeshするまずExcel.Application

public class InteropHelper 
{ 
    public static void GetReferences(ref Microsoft.Office.Interop.Excel.Application _Application, ref Microsoft.Office.Interop.Excel.Workbook _Workbook) 
    { 
     EnumChildCallback cb; 
     // First, get Excel's main window handle. 
     int hwnd = (int)Process.GetCurrentProcess().MainWindowHandle; 

     // We need to enumerate the child windows to find one that 
     // supports accessibility. To do this, instantiate the 
     // delegate and wrap the callback method in it, then call 
     // EnumChildWindows, passing the delegate as the 2nd arg. 
     if (hwnd != 0) 
     { 
      int hwndChild = 0; 
      cb = new EnumChildCallback(EnumChildProc); 
      EnumChildWindows(hwnd, cb, ref hwndChild); 

      // If we found an accessible child window, call 
      // AccessibleObjectFromWindow, passing the constant 
      // OBJID_NATIVEOM (defined in winuser.h) and 
      // IID_IDispatch - we want an IDispatch pointer 
      // into the native object model. 
      if (hwndChild != 0) 
      { 
       const uint OBJID_NATIVEOM = 0xFFFFFFF0; 
       Guid IID_IDispatch = new Guid(
        "{00020400-0000-0000-C000-000000000046}"); 
       Microsoft.Office.Interop.Excel.Window ptr = null; 

       int hr = AccessibleObjectFromWindow(
         hwndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), ref ptr); 
       if (hr >= 0) 
       { 
        // If we successfully got a native OM 
        // IDispatch pointer, we can QI this for 
        // an Excel Application (using the implicit 
        // cast operator supplied in the PIA). 
        _Application = ptr.Application; 
        _Workbook = _Application.ActiveWorkbook; 
       } 
      } 
     } 
    } 

    [DllImport("Oleacc.dll")] 
    public static extern int AccessibleObjectFromWindow(
      int hwnd, uint dwObjectID, byte[] riid, 
      ref Microsoft.Office.Interop.Excel.Window ptr); 

    public delegate bool EnumChildCallback(int hwnd, ref int lParam); 

    [DllImport("User32.dll")] 
    public static extern bool EnumChildWindows(
      int hWndParent, EnumChildCallback lpEnumFunc, 
      ref int lParam); 


    [DllImport("User32.dll")] 
    public static extern int GetClassName(
      int hWnd, StringBuilder lpClassName, int nMaxCount); 

    public static bool EnumChildProc(int hwndChild, ref int lParam) 
    { 
     StringBuilder buf = new StringBuilder(128); 
     GetClassName(hwndChild, buf, 128); 
     if (buf.ToString() == "EXCEL7") 
     { 
      lParam = hwndChild; 
      return false; 
     } 
     return true; 
    } 
} 
+0

私はにVSTOを使用していないUDFと呼ばれる範囲を返します。私のアドインを作成し、私はAC#クラスライブラリとしてオートメーションアドインを実装しました。だから、 "Globals.ThisAddIn.Application.Caller"が私のために働いていないのです。または、名前空間宣言がありませんか? – shrikanth

+0

comを使用してExcel.Applicationを参照できます –

0

感謝を取得するには、このような何か。私は、セルのアドレスを取得するには、以下の使用:

using Excel = Microsoft.Office.Interop.Excel; 
using System.Reflection; 

Excel.Application excelApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application"); 

Excel.Range target = (Excel.Range)excelApp.get_Caller(System.Type.Missing); 

string cellAddress = target.get_Address(Missing.Value, Missing.Value, 
      Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value); 
関連する問題