Create Dynamic SpinButton and add spinup procedure
-
Sonntag, 29. Juli 2012 13:05
I have a form called "UserForm1". It has 5 preset controls.
I wish to dynamically add a spinbutton to the form, and then the proper modules for the spinup and spindown events.
I am adding the controls via coding that resides in a separate module ("Module1"), not withing the actual form itself.
I have using the following code in my vain attempt to insert the dynamic events coding.
Set CODEMOD = ActiveDocument.VBProject.VBComponents.VBE.ActiveCodePane.CodeModule
I soon realized that the reference is to module "Module1" and when I add lines of code, it goes there, not to the bottom of the code for "UserForm1". So I get errors.
How do I point to the VBComponents for "UserForm1" (I have tried substituting "UserForm1" for ActiveCodePane, and lots of other things, but nothing yet works.
Roy Lasris
Alle Antworten
-
Sonntag, 29. Juli 2012 15:38
Use
Set CODEMOD = ActiveDocument.VBProject.VBComponents("UserForm1").CodeModuleRegards, Hans Vogelaar
-
Sonntag, 29. Juli 2012 16:58
I had tried that to no avail. When I put it in quotes, it returns Error 9 "Subscript out of range". When I use it without quotes it returns Error 13 'Type Mismatch'.
FWIW, when I put this in the code:
For Each ocomponent In ActiveDocument.VBProject.VBComponents
a=ocomponent.name
next
and then cycle though the code I get this I only get "This Document" as a component (suggesting that the document, and not the attached macro, is being polled). In fact I have a hundred components in my vbproject (called myProject or vbmyProject) attached to the document.
Roy Lasris
-
Sonntag, 29. Juli 2012 17:48Is the userform stored in the active document or in the template attached to the document?
Regards, Hans Vogelaar
-
Sonntag, 29. Juli 2012 18:16
A template attached to the document. Sorry, I should have said that at the beginning.
I think I am onto something, however.
Set CurrProj = VBE.ActiveVBproject.VBComponents
seems to give me the current project. At least I am able to get the code set that I want. Is this a proper way to access the code?
Roy Lasris
-
Sonntag, 29. Juli 2012 18:22
Does this work for you?
Set CODEMOD = ActiveDocument.AttachedTemplate.VBProject _ .VBComponents("UserForm1").CodeModule
Regards, Hans Vogelaar
- Bearbeitet Hans Vogelaar MVPMVP Sonntag, 29. Juli 2012 18:23
-
Sonntag, 29. Juli 2012 18:56
I am afraid not. I get the same errors. But the text:
Set CurrProj = VBE.ActiveVBproject.VBComponents
does work and I can generate new code at the end of the form's code page.
Thank you for your help.
Roy Lasris
- Als Antwort markiert Leo_GaoModerator Freitag, 3. August 2012 01:19

