Autor Beitrag
ocram1
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: Mi 09.05.12 23:52 
Hallo,
ich möchte mit Visual Studio Express ein Add-In für Excel 2007 erstellen.

Folgenden Code habe ich:
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
using System;
using System.Runtime.InteropServices;

namespace NAddIn
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class Functions
    {
        public Functions()
        {
        }
        
        public double Add2(double v1, double v2)
        {
            return v1 + v2;
        }

        [ComRegisterFunctionAttribute]
        public static void RegisterFunction(System.Type t)
            

        {
            Microsoft.Win32.Registry.CurrentUser.CreateSubKey(
                "CLSID\\{" + t.GUID.ToString().ToUpper() + "}\\Programmable");
        }

        [ComUnregisterFunctionAttribute]
        public static void UnregisterFunction(System.Type t)
        {
            Microsoft.Win32.Registry.CurrentUser.DeleteSubKey(
                "CLSID\\{" + t.GUID.ToString().ToUpper() + "}\\Programmable");
        }
    }
}


Nach dem ich das mittels F5 mal ausgeführt habe öffen ich Excel und gehe zu Excel-Otionen -> Add-Ins -> Com-Add-Ins und wähle die .dll im Ordner Bin/Debug aus.
Danach kommt aber die Meldung
Zitat:

... .dll ist kein gültiges Offic-Addin


Weiss jemand was ich hier falsch mache.

Danke und Grüsse
ocram1 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: So 13.05.12 19:50 
In einem anderen Forum wurde mir nun geholfen.

Ich versuche das Erfahrene kurz weiter geben.

Hier mein Code:
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;

namespace NAddIn
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class Functions
    {
        public Functions()
        {
        }
        
        public double Adddbl(double v1, double v2)
        {
            return v1 + v2;
        }

        [ComRegisterFunctionAttribute]
        public static void RegisterFunction(System.Type t)
        {
            string guidPath = String.Format("CLSID\\{{{0}}}", t.GUID.ToString().ToUpper());
            string mscoreePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "mscoree.dll");
            string inprocServer32Path = String.Format("{0}\\{1}", guidPath, "InprocServer32");
            string programmablePath = String.Format("{0}\\{1}", guidPath, "Programmable");

            RegistryKey key = Registry.ClassesRoot.OpenSubKey(inprocServer32Path, true);

            key.SetValue("", mscoreePath);

            Registry.ClassesRoot.CreateSubKey(programmablePath);
        }
        [ComUnregisterFunctionAttribute]
        public static void UnregisterFunction(System.Type t)
        {
            string guidPath = String.Format("CLSID\\{{{0}}}", t.GUID.ToString().ToUpper());
            string inprocServer32Path = String.Format("{0}\\{1}", guidPath, "InprocServer32");
            string programmablePath = String.Format("{0}\\{1}", guidPath, "Programmable");

            RegistryKey key = Registry.ClassesRoot.OpenSubKey(inprocServer32Path, true);
            key.SetValue(String.Empty, string.Empty);

            Registry.ClassesRoot.DeleteSubKey(programmablePath);
        }

    }
}


Folgende Einstellung ist zu machen:
Projekt > Eigenschaften > Anwendung > Assemblyinformationenn... dort auf die CheckBox "Assembly COM-sichtbar machen" klicken.

Dann die Bibliothek erstellen:
Zitat:

Menü Erstellen, oder Strg+Umsch+B.


Dann die Registrierung durchführen:
Zitat:

RegAsm.exe kannst Du über einen Post-Build-Befehl automatisch ausführen lassen (Projekt > Eigenschaften > Buildereignisse):
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" "$(TargetPath)" /codebase


Dann Excel öffnen und die Funktion laden:
Zitat:

Automations-Add-Ins können über Datei > Optionen > Add-Ins > Verwalten: Excel-Add-Ins> Gehe zu... > Automatisierung... hinzugefügt werden.
Die Funktion heist dort: NAddIn.Functions


Grüße