.NET Core / .NET 6: Creating a TLB or DLL that can be added as reference in VBA - excel

I am trying to do basically what it says in the title: I have created a class library (dll) using .NET 6.0 and I would like to add that as a reference in an Excel/Access VBA document. I diligently followed the steps here:
https://learn.microsoft.com/en-us/dotnet/core/native-interop/expose-components-to-com
No matter what I try, I cannot add the resulting dll as a reference in Excel. I just keep getting the message: 'Can't add reference to the specified file'.
A bit more info: I am able to create the Assembly.comhost.dll file, and I think I have done everything correctly, but no joy.
This is rather frustrating, as doing this in .NET Framework is but a click of a couple of checkboxes. In .NET Core (.NET 6) It seems to be a bit of a nightmare. Also, being able to create a tlb from the dll was always a perfect confirmation that you would be able to add your reference in Excel.
ANY help is most welcome!
Thanks!

in the tutorial you hav ementioned, ther is a topic "Register the COM host for COM". here a file named "ProjectName.comhost.dll" is used to register the assembly to the registry as a COM type library.
After doing this, i could the assembly in VBS with the fowing conde:
set projectObject = CreateObject("ProjectNamespace.ProjectClass")
newValue = projectObject.ComputeNewValue("abc")
Right now, I was not able to establish a reference to that type library in my Excel 2016, but the code sample works in VBA as well.

I could also in VBA
Set projectObject = CreateObject("COMServer.Server")
MsgBox projectObject.ComputePi()
with this repository : https://github.com/dotnet/samples/tree/main/core/extensions/COMServerDemo
But that works only with Registered COM not with RegFree COM (Go check README).
Not every component is a suitable candidate for use under Reg-Free COM. A component is not considered suitable if any of the following are true:
The component is an out-of-process (ActiveX EXE) server. Only DLLs
are supported.
The component is a system component or part of the operating system,
such as XML, Data Access, Internet Explorer, or DirectX® components.
For example, you should not attempt to isolate components such as
Microsoft XML (MSXml.dll) or Microsoft Internet Controls
(SHDocVw.dll). These components are either part of the operating
system, or can be installed with a separate redistributable package.
The component is part of an application, such as Microsoft Office.
For example, you should not attempt to isolate components such as the
Microsoft Word Object Model or Microsoft Excel Object Model. These
two components are part of Office and can only be used on a machine
that has the full Office product installed on it.
The component is intended for use as an add-in or a snap-in, such as
an Office add-in or a control in a Web browser. Such components
typically require some kind of registration scheme defined by the
hosting environment that is beyond the scope of the manifest itself.
The other problem is an arbitrary application may not be designed to
recognize isolated components, as it probably doesn't have a way to
reference your component through a manifest.
The component manages a shared physical or virtual system resource.
For example, it could manage some kind of data connection shared
between multiple applications or a device driver for a print spooler.
Source : https://social.msdn.microsoft.com/Forums/en-US/cc08575d-3506-4a0f-a9e2-f23c2162ad38/using-net-excel-addin-with-registrationfree-com?forum=innovateonoffice
But I couldn't find a way to add it in Excel as a reference.

Related

QTP Addin - Object is not being recognised

QTP doesn't recognize item when I disable some add-ins which was checked when I added them in to the repository
Note: I actually selected all the add in's when I added the objects into repository - .Net, JAVA, Oracle, Siebel, SAP, Web Services, Silverlight, ActiveX(I know I am a Noob). When I just select Web right now and run the script, it's unable to recognize the object and when I add the object again(with only Web checked), it's being added as a new object and is being recognized when I run it.
So my question is what is the relationship of an object with respect to add in? How does it go on to affect the property of the same object?
Add-Ins are Collections of Pre-Defined Object Types
The add-in contains built-in property sets for a specific group of related UI objects. This helps QTP identify the runtime objects more effectively for a specific type of application.
For instance, it's quite predictable that browser-based applications will be using the standard HTML elements, so the basic web objects (i.e. Page, Frame, WebButton, WebList) have already been defined in advance inside the Web add-in.
The Evil in Enabling All Add-Ins
Most likely, you're facing the issue because all add-ins were enabled during recording. QTP had the option to choose from any of the pre-defined objects in all of those add-ins as it sees fit. You could have ended up with an object repository that's a hybrid of object types found in multiple add-ins.
We faced this similar issue before where a web button (i.e. html input type="button") was recorded as a SAPWebButton. That's because all add-ins were enabled during recording and no one noticed. It worked fine until we disabled all other add-ins, except for the Web add-in. Since the Web add-in only knows about a WebButton not a SAPWebButton, this resulted into identification problems.
Prevention and Fix
The correct approach is to decide which add-in is most appropriate for your application type before recording, enable it and disable the rest.
You will have to manually update the problematic object via Update Run Mode. If there's too much, you'd be better off recording from scratch.

Are Windows Security hide some methods from .NET classes?

I'm working under windows Server 2008 :S
with restricted user not administrator.
using Visual Studio 2010
I try to run write this code
ApplicationClass app = new ApplicationClass();
Workbook book = app.Workbooks.Open("");
Worksheet sheet = (Worksheet)book.Worksheets[1];
Picture pict = sheet.Pictures(pictureName) as Picture;
Pictures() method not exist in sheet object !
But
When I try this code under windows 7 with Administrator user
The Picture() method is exist and every thing is OK
I wonder, if there is some thing with interop and windows security ?
If you visit the MSDN page for this, I can see two issues:
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.pictures.aspx
This API supports the Visual Studio infrastructure and is not intended to be used directly from your code.
Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.
It seems the reason is indeed a restricted user. Is there a reason you are using this API since documentation suggests that it should not be used directly. Perhaps you are missing a wrapper.

Deploying custom dll's in SharePoint 2010

I'm new to SharePoint and trying to get my head around this. I have a simple Web Part project. I also have a custom Data layer project that uses the Microsoft Enterprise Library for data access. In the Web Part project, I am adding a reference to the Data layer project's assembly. I specified in the Package of the Web Part project that I want my Data layer's assembly to be deployed. I can verify this works by using standard ADO.NET classes and not the custom MS library. If I deploy to the SharePoint server (which I have 100% access to) using the Enterprise Library, I get the error message:
"Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.Data"
What steps do I need to take to ensure this project and all future projects on the server can easily gain access to the Enterprise Library for data access?
Thanks!
You did everything correct until a certain point: Deployment.
When deploying external DLLs, which shall also be put into the GAC or somewhere else, you need to package them with the WSP aswell. This has become very easy with Visual Studio 2010:
Open your Package
Click on "Advanced" (on the bottom)
Add your external DLL and maybe even SafeControls for the web.config

best content on how to deploy and share a VSTO solution

with the push to leverage visual studio and dotnet with office based solutions, especially excel, where is the best article or information on how having office sheet with additional binaries and assemblies is sharable.
Do this external code get packaged with the spreadsheet
what if people start emailing the spreadsheet around. Is there any overhead of this additional assemblies. Is there risk of the binaries getting detached from the spreadsheet
It seems like microsoft has been pushing VSTO for over 5 years now but you read lots of mixed reviews and issues. Are we at the point where companies that do large VBA excel solutions can fully migrate over to dotnet without any real worries?
First of all, I want to answer your question on whether or not VSTO is ready for larger implementations. The answer is YES! Especially if the alternative is VBA. You have the entire .Net framework available, you can use web services, ADO.Net (better still, with the enterprise library). You can still write code that looks a lot like VBA, but is much more powerful. You can get more information by reading Walkthrough: Creating Your First Document-Level Customization for Excel. This page will give you an idea of what VSTO features are available to you.
Now, to answer your question on deployment.
It depends on whether you are making an Add-In or a document level customization. If its an Add-In, then you must install it on each client, and any passing around of documents will not effect that (Add-Ins are at the application level, and not at the individual document level).
I assume that you are talking about a document level customization, so I will center my answer around that.
When you create a document level customization, the assemblies are not loaded into the excel file (as they are with VBA). Instead, a document property is added telling the application that this document contains a manifest file (and tells it of the manifest file's location). The manifest file contains links to the assembly that makes up your customization.
As with any .Net application, there are sometimes other (referenced) assemblies that also need to be deployed. Not always are these assemblies in the GAC, so they would have to be located in the same folder as your executing assembly (in this case, your customization assembly). You don't necessarily have to place your assemblies in the same location as your excel file, though.
There are a few ways you can deploy the customization.
You can store all of the assemblies and the excel file in a folder and run the application that way (if the excel file is passed around, the users must pass around the entire folder).
You can run an setup program that installs the assemblies to a specific folder on the users computer, and specifies the manifest at that location (if the excel file is passed around, the users must also pass around the setup program).
You can install the assemblies in a network location and specify in the document properties, that the manifest and the assemblies are all at that network location (if the excel file is passed around, nothing needs to be passed along with it - but there are security settings that need to be made. Read this page for more information).
Any way that you decide to do this, here is the page you need to read in order to understand the document properties that enable the customization.
You need to make sure all of your users have the prerequisites installed. The easiest way to do this is to give them all a setup program. If you create a setup project for your customization, you can setup the bootstrapper to automatically install the prerequisites. You can also do this if you use ClickOnce to install the customization. This page will give you all the information you need to know regarding deployment.
Here are some other helpful links you will need to see:
Application Manifests for Office Solutions (2007 System)
Deployment Manifests for Office Solutions (2007 System)
Application and Deployment Manifests in Office Solutions
I hope this helps. Once you read all of this information, I think you'll agree that VSTO is a much better choice than VBA. You just have to plan your deployment carefully.
I created a Excel add-on and deployed it to a server. Then I passed around two links, the setup.exe file and the link to the actual Workbook. The workbook file never changed - it's purpose was to open and load the add-on which was now in the user's Add/Remove Programs. The add-on itself would check for updates on startup.
So if they start emailing the workbook around, great! I believe in theory, the workbook would download the add-on if it was missing, but I don't have a fresh computer around to test that. If it doesn't, it's just a simply exe to install (which will update on launch anyhow, so outdated isn't an issue).
For document-level deployment, I recommend the following three links:
https://msdn.microsoft.com/en-us/library/ms268758(v=vs.90).aspx
https://msdn.microsoft.com/en-us/library/ff937654.aspx
How do you assign the Assembly Location of an Excel VSTO Installation?
Unlike application-level solution, document-level has 2 special properties: _AssemblyLocation and_AssemblyName, which indicate the *.vsto position. To set these properties by install path, a custom action project should be created to record the target path when installing, and packaged together with setup.exe. Then you could put your excel file anywhere (on local machine) and open it in the correct way.

Microsoft Communication control 6.0

Hello previously i was using VC++ 6.0 and MFC where i used Microsoft Communication Control 6.0 for serial communication its fine.But now i am using vc++.net 2003 and MFC, i added Microsoft Communication Control (MSComm1) but in class view there is no CMSComm class will creating . But in VC++ 6.0 if i add the this ActiveX Control.The CMSComm class will created by default where i can call member functions like SetPortOpen() , GetPortOpen() .
so any body tell me how to insert the MScomm control along with class.
Thanks in Advance
You may be having problems because this Microsoft update set the ActiveX killbit on the control. It will no longer function. MS suggests that we use a newer version of the control or the API.
EDIT: I did this with VC# and am not sure if the steps are the same with VC++. I need to install it and try it before providing a better response.
Since serial port support wasn't added to .Net until version 2.0 I had to do the same thing for .Net 1.1 apps. In my project I added a reference to MSCommLib and added the MS Communications Control, version 6.0, to my toolbox. After dragging one onto the form I was able to program against it. You should also have a reference to AxMSCommLib, AxInterop.MSCommLib.dll (COM interop not port).
Sorry, I opened the project and can't find a way to add it to the Class View, but once you have a reference and create a variable like "private AxMSCommLib.AxMSComm com;" you can use the intellisense to see the methods and members of the object.
You can also see these with the Object Browser. If you have the reference set, open the object browser and then click on AxMSComm. All of the members should be listed in the pane to the right side. There isn't much help for how to use each of the members here.

Resources