Share via

Counting userforms and items?

Anonymous
2012-07-31T13:28:00+00:00

Hi i am trying to count the amount of userforms in a document

and also the amount of fields on the userform. is this possible via VBA? Thanks

effectively want something like:

activedocument .forms .count

activedocument.forms.textbox.count

Thanks

Microsoft 365 and Office
Microsoft 365 and Office

A comprehensive suite of productivity tools and cloud services that enhance collaboration, communication, and efficiency. Combining classic Office apps with advanced Microsoft 365 features, it supports both personal and business needs

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

Answer accepted by question author

Andreas Killer 144.1K Reputation points Volunteer Moderator
2012-08-01T10:02:15+00:00

Set a reference to "Microsoft Visual Basic for Applications Extensibility" in the VBA editor, then execute the code below. It should be no problem to add a counter, right?

Andreas.

Sub Test()

  Dim VBcomp As VBComponent

  Dim C As Control

  'Visit each component in this file

  For Each VBcomp In ThisDocument.VBProject.VBComponents

    'What is it?

    Select Case VBcomp.Type

      Case vbext_ct_MSForm

        Debug.Print "Userform: "; VBcomp.Name

        'Visit the controls on this form

        For Each C In VBcomp.Designer.Controls

          Debug.Print "  Control: "; C.Name

        Next

    End Select

  Next

End Sub

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2012-08-02T10:11:10+00:00

    There is a help available for this in VBA, place the cursor on VBComponent and press F1.

    See also the help for CodeModule, explore the properties and methods for both. There are also some small examples.

    You can also find many example in the web, e.g. search for

    vba codemodule.addfromstring

    Andreas.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2012-08-02T09:20:18+00:00

    Thanks for this it's exactly what i wanted!

    Do you have any links to where i can read more about the extensions in  theVisual Basic for Applications Extensibility" reference sounds like it might be full of useful goodies for me!

    Was this answer helpful?

    0 comments No comments