VBA UserForm TextBox As Function Param

Anonymous
2018-06-08T00:25:20+00:00

I have a word 2016 doc and have created a userform with textboxes.

How do I pass a textbox as a parameter. Something like...

Public Function AAA(bbb as textbox)

I dont see a textbox or control type anymore.

Googling is no help

and Word help is even worse.

Thanks

Moved from: Office/Word/Windows 10/Office 2016

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. Jay Freedman 205.9K Reputation points Volunteer Moderator
    2018-06-10T03:28:25+00:00

    Passing controls as arguments isn't something I do, so I had to play with this a bit. It turns out that if you declare a sub or function with a parameter like this:

       Sub Toggle(ByRef ckBox As CheckBox)

    and try to use properties of the object ckBox in the procedure's code, you get (for most of the properties) an error that the object doesn't have any such property, or in some cases you'll get a type mismatch error at run time. It turns out that using just "CheckBox" like that gives you an entirely different kind of object with just a handful of properties.

    Instead, declare it this way:

        Sub Toggle(ByRef ckBox As MSForms.CheckBox)

    and everything works when you call it like

         Toggle CheckBox1

    3 people found this answer helpful.
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Jay Freedman 205.9K Reputation points Volunteer Moderator
    2018-06-08T02:29:39+00:00

    This code works for me:

    Private Sub CommandButton1_Click()

        Label1.Caption = readTb(TextBox1)

    End Sub

    Private Function readTb(tb As TextBox) As String

        readTb = tb.Value

    End Function

    In this case, the function is part of the userform's code. I don't think TextBox is a valid data type outside of a userform, though. If you need to pass it to a function in a regular module, just pass the individual property or properties of the text box that the function needs, as String or Long or whatever.

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2018-06-08T12:45:25+00:00

    Are you using Word 2016? I just compiled it and it complains about the "As Textbox". Is there a setting somewhere?

    0 comments No comments
  3. Jay Freedman 205.9K Reputation points Volunteer Moderator
    2018-06-08T17:20:15+00:00

    Yes, I'm using Word 2016 (Version 1803, Build 9126.2210 at the moment). However, this isn't version-dependent.

    The TextBox data type is defined in the Microsoft Forms 2.0 Object Library (in the file C:\Windows\SysWow64\FM20.DLL), which is referenced by default in a userform but not in a module. However, if you've already added a userform to your project, then the reference to FM20.DLL will also be available in a module in the same project.

    Check the references: Go to Tools > References and look for the Forms library in the checked items.

    If it is there, then it's possible that the FM20.DLL file is damaged. Running a repair of Office may (or may not) fix the problem.

    0 comments No comments
  4. Anonymous
    2018-06-10T02:12:01+00:00

    Yes I found it and it took care of the TextBox issue.

    Now when I compile it complains about CheckBox???

    It complains about 

        ckBox.Enabled

    Gez, this is driving me crazy

    0 comments No comments