VBA is equivalent to using C # or VB.NET import / create aliases

Base Link: Ten Code Conversions for VBA, Visual Basic.NET, and C #

Note. I already created and imported *.dll , this question is about aliases .

Let's say the program name of the Test class is TestNameSpace.Test

 [ProgId("TestNamespace.Test")] public class Test ... 

Now let's say that the C # solution was sealed and compiled into *.dll , and I refer to it in Excel VBE. Note: at the moment I cannot change the program name, as if *.dll not written by me.

This is VBA : instead of declaring such a variable:

 Dim myTest As TestNameSpace.Test Set myTest = new TestNameSpace.Test 

I would rather call it (still in VBE)

 Dim myTest As Test Set myText = new Test 

In C # you usually say

 using newNameForTest = TestNamespace.Test; newNameForTest myTest = new NewNameForTest; 

Note. Assume there are no namespace conflicts in a VBA project

Question: is there an equivalent call in VBA before C# using or VB.NET imports aliases?

+7
c # vba alias using-directives
source share
2 answers

Answer: There is no built-in VBE function that recognizes links added to the project and creates aliases at runtime (VBE runtime) if there are no name conflicts

In case of name conflicts in your registry all the points . are replaced by _ .

& Rdquo; ProgId (Software Identifiers)

In COM, it is used only in late binding. This is how to make a call to create a new object.

 Dim myObj = CreateObject("TestNamespace.Test") 


& Rdquo; EarlyBinding and LateBinding

In an early binding, you specify the type of object to create using the new keyword. The name of your object should appear in VBA intellisense. This has nothing to do with ProgId . To get the actual namespace used for your object type, open Object Explorer F2 and find it there

In this article, explain where the names in the early binding section came from.
use the same link when use late binding

for the MSDN softkey section see this

+2
source share

An interesting question (constantly using them, but never thought about their exact meaning). The definition of the Imports definition (the same for using ) is pretty clear: its only function is shortening links by deleting the corresponding Namespaces. So the first question asked by the question is: does VBA have such a thing (namespaces) at all? And the answer is no, as you can read from several sources; examples: Link 1 Link 2

In conclusion, without finding a single link to any VBA statement that does something similar to Imports / using , and confirming that VBA does not consider the β€œstructure” justifying their use (namespaces), I believe that I can say: No, this does not happen in VBA.

In addition, you should keep in mind that it would not have real applicability. For example: when converting VB.NET code, where Imports can be used, for example:

 Imports Microsoft.Office.Interop.Word ... Dim wdApp As Application 

the code will be completely changed, so the resulting string will not be long:

 Dim wdApp As Word.Application ' Prefacing the library display name. 

I think this is a good graphical reason explaining why VBA does not need such things: VB.NET takes into account many realities that need to be properly classified (namespaces); VBA takes into account a much smaller number of situations and, therefore, can afford not to perform such a systematic, long-named classification.

-------------------------- THE CONFIRMATION

Imports / using is a simple abbreviation for the name, that is, instead of writing any.whatever2.whatever3 every time you use the object of this namespace in Module / Class , you add Imports / using at the beginning, which basically means: "for all members of the X namespace, just forget about all the bla, bla headers. "

I am not saying that you cannot emulate such behavior; just emphasizing that having built-in functions for short names makes sense in VB.NET, where names can become very long, but not so much in VBA.

+3
source share

All Articles