VBA - Import namespaces?

Is it possible to import namespaces in VBA? I am writing Word macros and I want to put all the functions inside modules or classes. But as soon as I do this, I see that I cannot easily get the controls in the VBA form. I must first specify the name of the form, then access the control within this. I can't just go

TextBox1.Caption 

I need to go

 Form1.TextBox1.Caption 

Is there any way around this?

+1
vba module namespaces
source share
1 answer

You cannot bind your classes / modules so tightly. A work module should not require knowledge of the Form1 class, because then it cannot be used separately in another project.

Instead, you probably want to pass the function in your helper class the argument on which it does the work, and then returns the result (if necessary). As a completely useless, trivial example:

 Public Sub SetLabelText(ByVal lbl As Label, ByVal caption As String) lbl.Caption = caption End Sub 

And you would call it from a form class, for example:

 MyHelpers.SetLabelText(Label1, "New Label Caption") 

This way you can use functions in your helper class from any form.


But as far as your real question is, no. VBA does not have a concept of a namespace. The best thing you can do is with the statement, but often doing this is most often a sign of a design flaw, as I mentioned above.

+4
source share

All Articles