1

I'm writing a COM DLL for use in VBA using VB.Net. Here it is, in all its glory:

Public Class CompPl32
    <Runtime.InteropServices.ComVisibleAttribute(True)>
    Public Function MyUncompress(SrcSt As String) As Integer
        Return 0
    End Function
End Class

When I look for this in Excel's VBA editor, I find the dll/tlb with the projectname/rootnamespace "CompPL'. When I refer to it in code though, it's...

Dim pl As CompPl.CompPl32

Which is kind of ugly when you're trying to match the API of non.Net COMs. Is there a way to remove one level of the naming so it's either just CompPl or CompPl32? I tried putting it in a Module but you can't import just the project namespace, it has to have a class name in VBA, and changing the root namespace to nothing results in a blank string that is confusing.

Community
  • 1
  • 1
Maury Markowitz
  • 9,082
  • 11
  • 46
  • 98
  • Nothing related. Just I want to know why the downvoter did not write any comment? – Venu GoPal Feb 12 '18 at 20:26
  • I was curious about that myself. – Maury Markowitz Feb 12 '18 at 20:28
  • Or why even downvote? It seems like a reasonable question, spelling and grammar are good, not an obvious google-able answer, etc... – Jacob H Feb 12 '18 at 21:41
  • COM references are in the form of LibraryName.ClassName. I have no idea what "Which is kind of ugly when you're trying to match the API of non.Net COMs." means. However, Excel's VBA environment provides an Library lookup analogous to the VB.Net `Imports Namespace` statement for the Excel and VBA libraries. This feature does not preclude from fully qualifying the reference. If you are using multiple libraries with classes that have the same name as the default imported ones, you have no choice but to do so. i.e. `Dim wdApp as Word.Application` and `Dim xlApp as Excel.Application`. – TnTinMn Feb 13 '18 at 00:18
  • @TnTinMn the link appears to state the answer is no, but your statement above that appears to state yes. Do you recall the Library lookup statement you were thinking of? it sounds like what I'm looking for. – Maury Markowitz Feb 13 '18 at 16:41
  • My first comment was not meant to imply that the answer is "Yes you can" and I do not know the mechanism the IDE uses to expose its built-in _imports_, What it was meant to convey (and very poorly done :(), is that the concept of Namespaces does not exist in the VBA context and all classes (types) in an assembly (Library) are exposed directly from the assembly. Those classes accessible without a library qualifier (exposed from: Excel, VBA) are a builtin feature of the IDE that is not exposed to user. – TnTinMn Feb 13 '18 at 17:31
  • Ahhh I see what you are saying. I guess I'm back to my original attempt to solve this then, but that was downvoted to oblivion. – Maury Markowitz Feb 13 '18 at 17:37
  • Is there any technical issue in using the `Library.Type` notation, or is it just an inconvenience? Btw there are many, many things which a .Net dev may wish VB6/VBA had when stepping back in time. We're thankful that VB.Net came and improved upon VB6. – djv Feb 13 '18 at 18:29
  • It's really just an inconvenience, I've been asked to replace a very old piece of code with something 64-bit compatible, yet keep the API as much as possible so we don't have to change the VBA much. Using the Dim/Set and Library.Type will mean more changes in the VBA - not fatal, but as you say, who wants to step back in time? – Maury Markowitz Feb 13 '18 at 18:34
  • Ok, now I _think_ I understand what the real issue is. It is syntax changes required due to replacing a [Declared Function](https://stackoverflow.com/questions/48674115/do-i-have-to-register-a-dll-to-use-it-in-excel) with a .Net COM library. If this _is_ the issue, then look at the [Link 2](http://bytecomb.com/organizing-code-with-namespaces-in-vba/) in the referenced duplicate. It shows a method of creating a _default instance_ that I think will satisfy your requirements. – TnTinMn Feb 13 '18 at 18:59

0 Answers0