oleg-shilo / cs-script.npp

CS-Script (C# Intellisense) plugin for Notepad++ (x86/x64)
MIT License
246 stars 52 forks source link

How do I connect the dll? #54

Closed it19862 closed 7 months ago

it19862 commented 3 years ago

I'm getting an error: Error CS0433: Type 'Application2' exists in both 'Interop.Microsoft.Office.Interop.OneNote, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null' and 'Microsoft.Office.Interop.OneNote, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'.

I took the library from Visual Studio. 01

I placed the library as shown in the picture. MSACCESS_2021-07-06_22-47-38 02

Code How_to_connect_dll_01.cs

//css_reference ../Folder_dll/Interop.Microsoft.Office.Interop.OneNote.dll

using System;
using Microsoft.Office.Interop.OneNote;

namespace Folder_NS_02
{
    class  Test
    {
        static void GetNamesAllBooksInClipboard2()
        {
            var onenoteApp = new Application2();        
        }       
    }
} 
oleg-shilo commented 3 years ago

The error indicates that you are referencing two dll's: 'Interop.Microsoft.Office.Interop.OneNote, Version=1.1.0.0' and 'Microsoft.Office.Interop.OneNote, Version=15.0.0.0'.

It might be because VS created for a collable wrapper and cs-script picks them both wrapper and the assembly when it tries to resolve nemespace Microsoft.Office.Interop.OneNote into assembly.

The easiest way to check it is to try this:

//css_reference ../Folder_dll/Interop.Microsoft.Office.Interop.OneNote.dll

using System;

namespace Folder_NS_02
{
    class  Test
    {
        static void GetNamesAllBooksInClipboard2()
        {
            var onenoteApp = new Microsoft.Office.Interop.OneNote.Application2();       
        }       
    }
}

This may solve your problem but I would recommend you to go with dynamic instantiation instead even if the trick shared:

dynamic outlook = Activator.CreateInstance(Type.GetTypeFromProgID("Outlook.Application"));

dynamic email = oApp.CreateItem(0);
email.To = "me@gmail.com";
email.Subject = "test subject";
email.Body = "test body";

email.Send();

Then you do not need to reference any assemblies at all. works.

it19862 commented 3 years ago

@oleg-shilo I just did it this way.

using System;
using Microsoft.Office.Interop.OneNote;

namespace Folder_NS_02
{
    class Test
    {           
        static public void Main(string[] args)
        {
            var onenoteApp = new Application2();                    
            string currentNotebookId = onenoteApp.Windows.CurrentWindow.CurrentNotebookId;   
            Console.WriteLine(currentNotebookId);           
        }
    }   
}

It works

oleg-shilo commented 3 years ago

great