Is it possible to import namespaces in VBA? I’m writing Word macros and I want to put all functions inside Modules or Classes. But once I do that I see that I cannot reach the controls in the VBA form easily. I have to include the name of the form first, then access the control within that one. I can’t just go
TextBox1.Caption
I have to go
Form1.TextBox1.Caption
Is there a way to get around this?
You shouldn’t be coupling your classes/modules so tightly. A worker module shouldn’t require knowledge of the
Form1class, because then it can’t be used separately in another project.Instead, you probably want to pass the function in your helper class an argument on which it performs work, and then returns the result (if necessary). As a completely useless, trivial example:
And you would call it from within the form class, like so:
That way, you can use the functions in your helper class from any form.
But, as far as your actual question, no. VBA doesn’t have any concept of “namespaces”. The best you can do is the
Withstatement, but having to do this frequently is more likely an indication of a design flaw, as I discussed above.