PDA

View Full Version : Create userForm by code



ly47
04-08-2010, 02:36 AM
Hi
How to create UserForms by code(at runtime)
Thanks
ly

Bob Phillips
04-08-2010, 02:54 AM
My advice is - don't. It creates more problems than anyone needs in one lifetime, it is better to build it at design time.

ly47
04-08-2010, 03:33 AM
My advice is - don't. It creates more problems than anyone needs in one lifetime, it is better to build it at design time.
Hi
Thanks.
I found some codes on the net but they don't work even I added the needed libraries.
With VB.NET(2008) I did it and it is very simple and works fine(multiple forms)
If you can please give an example and if it makes problems I'll not use it.
Thanks
ly

GTO
04-08-2010, 09:04 AM
Greetings ly,

I'm not even in the same league as Bob as far as knowledge (more like high school second string baseball compared to a major league player), but for what its worth, I certainly agree. Code-writing code seems a hassle.

That said, its always neat to learn what all can be done :-)

Check out this kb article:

http://www.vbaexpress.com/kb/getarticle.php?kb_id=224

Hope that helps,

Mark

Bob Phillips
04-08-2010, 09:49 AM
Mark,

Not to disagree with you, but I don't object to code writing code I think that is a great idea, I do it all of the time. Check out some of my posts on the subject http://msmvps.com/blogs/xldynamic/archive/2009/08/18/xml-is-such-a-pain.aspx and http://msmvps.com/blogs/xldynamic/archive/2009/08/24/autogen-ed-ribbon-code.aspx.

What I think is not worth the hassle is generating objects, and their associated code, at run-time.

ly47
04-08-2010, 10:37 AM
Hi Mark
Thanks for the code.
I tried it and still not worked,so I searched at excel option(Excel 2007)
and at:Macro Setting I checked: Trust access to VBA project object model, and it Works ! - (That was the problem before I asked)
Other codes to create userForm by code now works.:beerchug:
Thanks a lot
ly

GTO
04-09-2010, 06:35 AM
Greetings ly,

...I checked: Trust access to VBA project object model, and it Works ! - (That was the problem before I asked)
Well shucks, sorry I didn't understand what was being the issue. Kudos on your tenacity and glad you fixed it:thumb .

GTO
04-09-2010, 11:10 AM
Hi Bob,

I am happy to be corrected, as of course I did not intend to mis-state your opinion. I did look at both of your articles, actually several times. My sense has always been that you may be a tad uncomfortable with overly complimentary comments, so I will be brief. Damn! The XML code is total Greek to me at this juncture, but your articles are informative and easy to follow.

I spoke too 'generically,' lacking specificity in my comment and making it mis-leading. I was thinking in regards to code writing code (cwc) in the distributed workbook, not cwc to assist in developing the workbook to be distributed. Even with not understanding the XML code, I believe I did grasp the priciple goal of the articles to do just that; assist in the development or editing/updating a project as needed. For what its worth, I certainly agree.

It is neat that the articles happen to deal with the ribbon (which as I understand, replaces the menubar? Damn is it that tall?!), as the one thing that I've tried thus far (or at least what the blonde brain recalls) for automating development was creating an add-in to insert a table driven commmandbar builder and a 'shortcut key defeater'. Quite an opposite need on my part, as rather than automating something you would be doing often, in my case, not often enough to remember stuff - so the table would have drop-downs to help me remember.

Thanks for both the correction and the links :-)

Mark

Bob Phillips
04-09-2010, 11:38 AM
Wasn't meaning to correct Mark, just to state unequivocally that I am a BIG fan of code writing code (Autogen as I call it). I met a guy last year on a job, and our irst converstaion as he picked me up from the airport was about AUtogen, he was doing a lot, I was doing a lot, he had just developed it into a whole framework.

That sort of menu builder that you describe is a classic, but I would venture it is not AUtogen or CWC really, it is just automating a task (what computers are great for, but don't you just find it a tad paradoxical that we spend most of our time writing code to automate tasks to write code - instead of getting on with the real work?), have a repeatable code procedure rather than repeatedly coding the commandbars.

The ribbon is actually very simple, limited but simple. You just have to get your brain wrapped around the new paradigm, which includes XML to define the ribbon, callbacks, and the like. I have written a table driven ribbon generator, much like your table driven commandbar builder, but this is Autogen as you cannot build it n the fly as you can with commandbars.

BTW, I am not uncomfortable with compliments, I just don't find them necessary, I have quite a big enough ego without needing it boosting :). In many cases a compliment is only an acknowledgement by person A that person B knows more at this point in time. Not saying one shouldn't use them, its a cultural prerogative, I just like to keep them in perspective in my own case.