1

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?

Kenny Bones
  • 5,017
  • 36
  • 111
  • 174

1 Answers1

4

You shouldn't be coupling your classes/modules so tightly. A worker module shouldn't require knowledge of the Form1 class, 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:

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

And you would call it from within the form class, like so:

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

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 With statement, but having to do this frequently is more likely an indication of a design flaw, as I discussed above.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574