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

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Calling a procedure in a module from a user form (Word 2003 VBA)

    Author
    Topic
    #409967

    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

    Viewing 5 reply threads
    Author
    Replies
    • #877679

      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

    • #877680

      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

    • #877683

      oops blush I forgot to attach the Word Doc sorry …

    • #877684

      oops blush I forgot to attach the Word Doc sorry …

    • #877685

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

    • #877686

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

    Viewing 5 reply threads
    Reply To: Reply #877679 in Calling a procedure in a module from a user form (Word 2003 VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel