如何在通过C#创建的Excel公式中传递Range对象?

时间:2019-01-21 11:21:53

标签: c# excel-formula

我已经通过以下链接使用帮助创建了Excel公式:Canonical: How to call .NET methods from Excel VBA

它工作正常,但是现在我需要将Excel范围对象发送到此公式,C#并在该范围上进行操作。每当我尝试这样做时,它都会给我“ #Value!”。错误。

我该怎么做?

(我在C#端而不是在VBA中编写公式的代码。请注意。)

我尝试使用普通的“ Microsoft.Office.Interop.Excel.Range”对象。

namespace FormulaAddin
    {
    [Guid("29A34EFD-B8A3-48D8-847A-4BF402831617")]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public class Formulae
        {

        public Formulae ()
            {
            }

        public int CountRange(Excel.Range nRange)
            {            
            //Some code working on the range
            }       

        #region Registering COM Object

        [ComRegisterFunction]
        public static void RegisterFunction (Type type)
            {
            Registry.ClassesRoot.CreateSubKey(GetSubKeyName(type, "Programmable"));
            RegistryKey key = Registry.ClassesRoot.OpenSubKey(GetSubKeyName(type, "InprocServer32"), true);
            key.SetValue("", Environment.SystemDirectory + @"\mscoree.dll", RegistryValueKind.String);
            }

        [ComUnregisterFunction]
        public static void UnregisterFunction (Type type)
            {
            Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(type, "Programmable"), false);
            }

        private static string GetSubKeyName (Type type, string subKeyName)
            {
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            s.Append(@"CLSID\{");
            s.Append(type.GUID.ToString().ToUpper());
            s.Append(@"}\");
            s.Append(subKeyName);
            return s.ToString();
            }

        #endregion
        }
    }

0 个答案:

没有答案