Results 1 to 14 of 14
  1. #1
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: How to create a new userform in a template? (Word97SR2)

    Welcome to hell. This, at least in Word2000, is a bizarre scoping error. The following approaches work:

    (1) hard code the new name

    usr.Name = "item6"

    (2) assign the new name to a public string variable

    Public str2 as String
    ----------
    Public Function vbcCreateForm(strName As String) As VBComponent
    ...
    usr.Name = str2
    ...
    ----------
    Sub TESTfrmCreateForm()
    ...
    str2 = "item6"
    ...
    ----------
    But passing the string in the completely normal manner does not. Explain that!

  2. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: How to create a new userform in a template? (Word97SR2)

    Chris, I tried your code and, not having or knowing anything about "optWZ", I used the active VBProject (Set usr = Application.VBE.ActiveVBProject.VBComponents.Add(v bext_ct_MSForm)), and your code worked no problem, in that I got a userform with the codename "item6". The caption still read "UserForm1", but that should be easy to fix. This was in Word2000.

    Just an observation.

    Andrew

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to create a new userform in a template? (Word97SR2)

    > your code worked no problem,

    Mea Culpa.

    I forgot to mention that it DOES work the first time, but fails on a second call, even after the UserForm has been removed (right-click, Remove)

    I'm going to essay with JScher's tactics and will report back.

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to create a new userform in a template? (Word97SR2)

    > (1) hard code the new name

    This I can't do; I need to build forms dynamically, with identifiers (Name, Caption etc) based on incoming data.


    > (2) assign the new name to a public string variable

    Hmmm. I tried this, faithfully, pasting your "str2" code as is (sans comments) so that my module had a public str2, and I set that in the TEST sub before calling the function. The function uses the public str2 to assign the name "item6" - and I have no "item6" visible, just some old "UserForm1"s and the latest "Item13" and on, but still get the path/file error.

    I assign str2="Item17" (never been used) and the code runs to completion.

    I remove the userform "Item17" and rerun the test, which fails with path/File error.

    I'm going to bed, to sleep, perchance to dream (but not like last night, I hope!)


    My pet theory is that right-click remove is NOT clearing out entries in the symbol table. VBE still thinks that it has a Item17 in hand.

    (couldn't sleep ...)

    If I create Item18, runs fine, Remove it, code fails. Save the template and exit/reload word and then I can create Item18 again. Once. That behaviour suggest to me that the "Item18" is indeed properly removed from the template, but that a trace of it exists in (volatile) working memory, and so disappears when Word is unloaded, and is not present when Word is reloaded.


    At the risk of boring everyone to death, here's the current code.

    <pre>Public Function vbcCreateForm() As VBComponent
    ' Procedure : vbcCreateForm
    ' Description: Create a userform.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: STRING name of a form
    ' Returns: USERFORM.
    ' Assumes: Nothing
    ' Side Effects: None.
    ' Tested: By the calls shown below.

    Dim usr As VBComponent ' tried this "as userform" and "as object" and ""
    ' (help File) "For the VBComponents collection, the Add method returns a VBComponent object. "
    Set usr = Application.VBE.VBProjects("optWz").VBComponents.A dd(vbext_ct_MSForm)
    ' MsgBox usr.Name ' shows variously as Userform1, UserForm2 etc.
    ' 32813' Can't move focus to the control because it is invisible, not enabled, _
    ' or of a type that does not accept the focus.
    ' then 32813 Method 'Name' of object '_VBComponent' failed
    ' then 75' path file error " ???!!!???
    ' usr.Name = strName
    usr.Name = str2
    ' It is as if the name, once used, is lodged in VBE's symbol table.

    Set vbcCreateForm = usr
    End Function
    Sub TESTvbcCreateForm()
    ' I want to create a userform and be able to interrogate it once it is created.
    '
    'Welcome to hell. This, at least in Word2000, is a bizarre scoping error.
    ' The following approaches work:
    ' (1) hard code the new name
    ' usr.Name = "item6"
    ' (2) assign the new name to a public string variable
    ' Public str2 As String
    ' ----------
    ' Public Function vbcCreateForm(strName As String) As VBComponent
    ' ...
    ' usr.Name = str2
    ' ...
    ' ----------
    ' Sub TESTfrmCreateForm()
    ' ...
    ' str2 = "item6"
    ' ...
    ' ----------
    ' But passing the string in the completely normal manner does not. Explain that!

    Dim strName As String
    strName = "item15"
    str2 = "item18"
    Dim iVBC As Integer
    For iVBC = 1 To Application.VBE.ActiveVBProject.VBComponents.Count
    Debug.Print Application.VBE.ActiveVBProject.VBComponents(iVBC) .Name
    If Application.VBE.ActiveVBProject.VBComponents(iVBC) .Name = strName Then
    MsgBox "can't do it at " & iVBC
    Else
    End If
    Next iVBC
    Dim vbc As VBComponent
    Set vbc = vbcCreateForm()
    MsgBox vbc.Name
    With Application.VBE.ActiveVBProject.VBComponents(vbc.N ame)
    MsgBox .Properties("caption")
    MsgBox .Name
    .Name = str2
    .Properties("caption") = "cap" & strName
    End With
    MsgBox vbc.Name
    End Sub
    </pre>


  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to create a new userform in a template? (Word97SR2)

    How to create a new userform in a template and then do something with it?

    I want to create a series of userforms and build them via VBA. At least I'll want to fere to them as some sort of object while I add command buttons, labels and textboxes to them.

    However, I can't get past the step of assigning a mmeaningful name to the form, as the example below shows.



    <pre>Public Function vbcCreateForm(strName As String) As VBComponent
    ' Procedure : vbcCreateForm
    ' Description: Create a userform.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: STRING name of a form
    ' Returns: USERFORM.
    ' Assumes: Nothing
    ' Side Effects: None.
    ' Tested: By the calls shown below.

    Dim usr As VBComponent ' tried this "as userform" and "as object" and ""
    ' (help File) "For the VBComponents collection, the Add method returns a VBComponent object. "
    Set usr = Application.VBE.VBProjects("optWz").VBComponents.A dd(vbext_ct_MSForm)
    MsgBox usr.Name ' shows variously as Userform1, UserForm2 etc.
    usr.Name = strName ' " '75' path file error " ???!!!???
    Set vbcCreateForm = usr
    End Function
    Sub TESTfrmCreateForm()
    ' I want to create a userform and be able to interrogate it once it is created.
    Dim vbc As VBComponent
    Set vbc = vbcCreateForm("item6")
    MsgBox vbc.Name
    End Sub
    </pre>


  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to create a new userform in a template? (Word97SR2)

    > The following approaches work:

    Thanks, I'll try those.

    I came back online to report further clues - I'm still trying to work out what is going wrong. The sample code below illustrates that it is AS IF VBE has remembered the name of the user form after it has been deleted/removed!

    You will see that I have been rolling up the names, and am at Item15 by now. My next test would be Item16, and so on.

    I run the TEST procedure with a brand new name string (say "Item16") and everything works just fine!

    I remove the userform (right-click, remove userform) and run the test a second time and get that "path" error. Sometimes other errors, as shown in the code comments.

    You will see also that I have instituted a little loop to test for the presence of the identifier in VBE's symbol table. That's not a fix, it's just me being curious.

    As well I may be confused between the form itself as a UserForm, and the form as a VBComponent. The caption clearly is another level in, as .Properties shows.


    <pre>Public Function vbcCreateForm() As VBComponent
    ' Procedure : vbcCreateForm
    ' Description: Create a userform.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: STRING name of a form
    ' Returns: USERFORM.
    ' Assumes: Nothing
    ' Side Effects: None.
    ' Tested: By the calls shown below.

    Dim usr As VBComponent ' tried this "as userform" and "as object" and ""
    ' (help File) "For the VBComponents collection, the Add method returns a VBComponent object. "
    Set usr = Application.VBE.VBProjects("optWz").VBComponents.A dd(vbext_ct_MSForm)
    ' MsgBox usr.Name ' shows variously as Userform1, UserForm2 etc.
    ' 32813' Can't move focus to the control because it is invisible, not enabled, _
    ' or of a type that does not accept the focus.
    ' then 32813 Method 'Name' of object '_VBComponent' failed
    ' then 75' path file error " ???!!!???
    ' usr.Name = strName
    ' It is as if the name, once used, is lodged in VBE's symboltable.

    Set vbcCreateForm = usr
    End Function
    Sub TESTvbcCreateForm()
    ' I want to create a userform and be able to interrogate it once it is created.
    Dim strName As String
    strName = "item15"
    Dim iVBC As Integer
    For iVBC = 1 To Application.VBE.ActiveVBProject.VBComponents.Count
    Debug.Print Application.VBE.ActiveVBProject.VBComponents(iVBC) .Name
    If Application.VBE.ActiveVBProject.VBComponents(iVBC) .Name = strName Then
    MsgBox "can't do it at " & iVBC
    Else
    End If
    Next iVBC
    Dim vbc As VBComponent
    Set vbc = vbcCreateForm()
    MsgBox vbc.Name
    With Application.VBE.ActiveVBProject.VBComponents(vbc.N ame)
    MsgBox .Properties("caption")
    MsgBox .Name
    .Name = strName
    .Properties("caption") = "cap" & strName
    End With
    MsgBox vbc.Name
    End Sub
    </pre>


  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: How to create a new userform in a template? (Word97SR2)

    I didn't test it twice? Oops.

    So "Remove" leaves debris, or so it appears. Is this likely to be a problem in the real world? If not, then, maybe we should not lose (further) sleep over it.

    On the other hand, you could add in some error handling code to arbitrarily append digits to the name to work around it:

    <pre>Public Function vbcCreateForm() As VBComponent
    ' Procedure : vbcCreateForm
    ' Description: Create a userform.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: STRING name of a form
    ' Returns: USERFORM.
    ' Assumes: Nothing
    ' Side Effects: None.
    ' Tested: By the calls shown below.

    Dim intIncrement As Integer
    intIncrement = 0
    Dim usr As VBComponent ' tried this "as userform" and "as object" and ""
    ' (help File) "For the VBComponents collection, the Add method returns a VBComponent object. "
    Set usr = Application.VBE.VBProjects("Project").VBComponents .Add(vbext_ct_MSForm)
    ' MsgBox usr.Name ' shows variously as Userform1, UserForm2 etc.
    ' 32813' Can't move focus to the control because it is invisible, not enabled, _
    ' or of a type that does not accept the focus.
    ' then 32813 Method 'Name' of object '_VBComponent' failed
    ' then 75' path file error " ???!!!???
    ' usr.Name = strName
    On Error GoTo nameIncrement
    usr.Name = str2
    ' It is as if the name, once used, is lodged in VBE's symbol table.

    Set vbcCreateForm = usr
    On Error GoTo 0
    Exit Function

    nameIncrement:
    If (Err.Number = 75) Or (Err.Number = 32813) Or (Err.Number = 50135) Then
    intIncrement = intIncrement + 1
    If intIncrement = 1 Then
    str2 = str2 & Trim(Str(intIncrement))
    Else
    str2 = Left(str2, Len(str2) - Len(Trim(Str(intIncrement - 1)))) & Trim(Str(intIncrement))
    End If
    Resume
    Else
    MsgBox Err.Number & " = " & Err.Description
    End If


    End Function</pre>

    I didn't work hard to increment integers already embedded in your form names, since I figured "item6" was arbitrary and for testing purposes. What you can do with this approach, going back to the original code, is pass a base string (e.g., myUserForm) and let the function do the work of numbering them. Certainly not the prettiest way of handling it, but given the Remove problem, perhaps the safest way.

  8. #8
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to create a new userform in a template? (Word97SR2)

    > Is this likely to be a problem in the real world?

    Define "real" (grin!)

    My real world late last night was struggling to understand why the darn thing worked the first time, but never again. You know, that feeling you get that maybe you just dreamed that it worked once upon a time in another world.

    Let's change the subject to "Things you need to find if you're having problems with adding controls and forms and have done a SEARCH on the VBA forum"


    I agree, lose no sleep over this one, but be aware that the symbol table isn't managed properly.


    FWIW today things are bopping along quite happily. The program has to build a new template each time, so once I put that code in place, the symbol table became a non-issue, since I start each test by deleting the old template by hand and recreating it in the program.

    That's odd. DELETING the template avoids the problem, so it now seems that when I REMOVE the item, the symbol table is left in place albeit corrupted, but when I DELETE the template (from the file system) my code is re-runnable. Therefore EITHER the symbol table is stored in the template during VBA editing OR Deleting the template triggers code that IS effective in removing the symbol table from the VBE.

    There's a PHd thesis in there for the right person ........

  9. #9
    5 Star Lounger
    Join Date
    Feb 2001
    Location
    Dallas plus 20 miles or so, Texas, USA
    Posts
    876
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to create a new userform in a template? (Word97SR2)

    Chris,

    Just a thought, but in several of my projects in Excel 2000, some of my problems with references to forms have been solved by saving the project, and then continuing. Long shot, but I thought I might mention it...

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to create a new userform in a template? (Word97SR2)

    >saving the project, and then continuing.

    Michael, thanks for this. I know that Excel/VBE differs from Word/VBE; nonetheless, it may be that the process of Saving the file is the equivalent of my exiting/entering Word - it triggers code that does resolve problems of a [artially updated symbol table or other device.

    It's all worth remembering, for future reference, that when something awkward happens, if re-opening the project clears the problem, it may not be a problem with the code at all.


    FWIW as I delve deeper into Word97 GUI forms, anmd their development at run-time, I find other aspects, such as the entries not being completed when I use the period key (I think it's called Intellisense, but I'm not sure). That suggests that the GUI forms part of VBE is generally poorly written.

  11. #11
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to create a new userform in a template? (Word97SR2)

    Chris,

    I'm intrigued by this topic because I have thought about doing this very thing so as to encapsulate user prompts in form controls that are created at runtime. It's a great idea.

    Just one thing I noticed that probably does not matter, but, heck here I am reading this, I might as well toss in my $1.98:
    I don't see anywhere where you destroy the object explicitly.

    Set usr = nothing
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  12. #12
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to create a new userform in a template? (Word97SR2)

    > I'm intrigued by this topic

    (Rough translation) "I have risen to the bait"

    > It's a great idea.

    (Now Chris rises to the bait)

    > where you destroy the object explicitly.

    Because I'm using Word, and the object gets destroyed when I have to reboot because Word hung, and in doing so, hung on to the document?

    nah. Because I just haven't got that far.


    So, tell me. Apart from freeing up memory, what will setting the usr (or any other object) to Nothing get me? It's not a big deal to effect it; I could grab all references to "Set <something> = " and go to work. Will this apply to Function results, or just objects which I have explicitly DIMmed?



    Background: I had to have a skeleton project on which to hang this concept. I chose to write an Options Wizard. You give it an INI file and the OptWz builds a set of user forms. The main form is based on the file, and has a button for each section of the INI file. Each section of the INI file has its own form, and each such form has a set of labels (key names) and text boxes (key values). There's a Save and Exit button on each form. You can refresh an entry by clicking on it, I think, or save an entry to the internal array where it is held. The Save button writes that section back to the INI file. I can't see why I shouldn't incorporate my greater-then-254 routines into it too.

    All in all, a pretty futile exercise, because who uses INI files nowadays, right? Still, turning it loose on c:windowswin.ini and c:windowssystem.ini taught me a thing or two about what constitutes a valid section name and does NOT constitute a valid user form name. Also programming scroll bars to forms but only where necessary. I seem to have resolved the best-fit problem in the placement of the labels and text boxes. Loading up the various event codes was easy.

    Let me know if you'd like to run a copy and I'll u/l to my web site.


    P.S. you're $1.98 counts for a lot up here. $3 last time I looked.

    P.P.S. in 640x480 256 colour resolution I can see grains of something under your fingernails. Go wash (grin!)

  13. #13
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to create a new userform in a template? (Word97SR2)

    I still have not finished analyzing your ini routines, but YES, send the url for this little diddy too.

    I'm going to go full tilt on your ini code very soon because I've got to see if I can improve on it. <img src=/S/smash.gif border=0 alt=smash width=30 height=26> Then, we'll talk about the proposal I referred to earlier...
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  14. #14
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to create a new userform in a template? (Word97SR2)

    try <A target="_blank" HREF=http://www.vif.com/users/cgreaves/optwz.zip>this</A>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •