Results 1 to 4 of 4
  1. #1
    Lounger
    Join Date
    Jul 2003
    Location
    Burnaby, Br. Columbia, Canada
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calling a procedure in a module from a user form (Word 2003 VBA)

    In the past I've called subs and functions (stored in regular old modules) from user forms. I do this because quite often I'll have a piece of code that will run in many user forms, for example to create user initials based on the name typed in, to populate common comboboxes, etc. Quite often my sub or function will require parameters, such as the user form it is being called from, or the control it should affect, etc. as these are required in the code, but may vary depending on the user form I want the code to affect.

    This has always worked very simply in the past...up to Word 2003 that is. Now, for some reason I can't seem to get these same pieces of code to work. They worked in Word 2002, but when I brought the code into Word 2003, no go. However, if I put the code as a sub IN the user form, the code works just ducky. But I don't want to put all this code in all my user forms, I want to just be able to call it as needed and keep it in one place.

    Following is a sample bit of code that works fine if placed in the User Form, but doesn't work if I call it (although, remember, it used to work in Word 2002).

    Public Sub PopInitials(objForm As Object)
    'Populates Initials with the first letter of each word in the Name

    On Error GoTo RoutineErr

    Dim sName As String
    Dim sInitials As String
    Dim nCount As Integer

    'If name is changed, recreates initials based on first letter of each word
    sName = objForm.cboAuthor.Value

    Do Until InStr(1, sName, " ") = 0
    sInitials = sInitials & Left(sName, 1)
    sName = Right(sName, Len(sName) - InStr(1, sName, " "))
    Loop

    sInitials = sInitials & Left(sName, 1)
    objForm.txtInitials.Value = sInitials

    RoutineExit:
    Exit Sub

    RoutineErr:
    clsErrorHandler.AssertError "basPopControls.PopInitials"
    Resume RoutineExit

    End Sub

    Does anyone have any ideas as to what has changed or what I have to do to make this work?

    As always, your help is invaluable,
    Karina

  2. #2
    Star Lounger
    Join Date
    Jun 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling a procedure in a module from a user form (Word 2003 VBA)

    I got it to work in Word 2003 by;
    In the form itself I used a commandbutton to launch it, and initialized objForm =Me

    Public objForm As Object

    Public Sub CommandButton1_Click()
    Me.txtInitials.Text = Module1.PopInitials(objForm.cboAuthor)
    End Sub

    In the Module, I made PopInitials a Public Function, passing it the Value of the combobox
    and returning the Initials to the calling routine.

    Public Function PopInitials(objForm As Object) As String
    ...
    sName = objForm

    I'm afraid that I'd never bothered passing a form's name to a sub so I can't comment
    on why its no longer working. I've attached the working example

  3. #3
    Star Lounger
    Join Date
    Jun 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling a procedure in a module from a user form (Word 2003 VBA)

    oops <img src=/S/blush.gif border=0 alt=blush width=15 height=15> I forgot to attach the Word Doc sorry ...

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Calling a procedure in a module from a user form (Word 2003 VBA)

    Hi,
    What is the code that calls the sub? Also, what error are you getting? (or does it simply do nothing?)
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

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