Results 1 to 7 of 7

Thread: MS Word VB Issue

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    3

    MS Word VB Issue

    Ok...I have a MS Words form. I created a textbox in this form.

    The other thing that I have is a popup "UserForm" with a listbox.

    When an user select any one value from this listbox, I want that value to populate the "textbox".

    Here are my codes:
    Private Sub CommandButton1_Click()
    ActiveDocument.FormFields("TextBox1").Result = Me.ListBox1.Text
    Unload Me
    End Sub


    WHen I run this, I get an '5941' error message. Please advise what I am doing wrong.

    Thank you.
    Joe

  2. #2
    Lively Member Vanquish2888's Avatar
    Join Date
    Sep 2007
    Posts
    104

    Re: MS Word VB Issue

    Hey joesmith!

    When I created a MS Word Form textbox, the default name was "Text1". Double-check that the box you're trying to modify is called TextBox1. Also are you running the code inside the userform? If not you will need to change the Me.ListBox1.Text to <insert userform name>.ListBox1.Text.

    Hope that helps ya!
    ~AJ

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    3

    Re: MS Word VB Issue

    Thanks AJ.

    I did make sure the text box is named correctly. But still not working.

    in my 'main' document, i am running:
    Private Sub txtLeon_GotFocus()
    UserForm1.Show
    End Sub


    In the UserForm1, I am running:
    Private Sub CommandButton1_Click()
    ActiveDocument.FormFields("txtLeon").Result = Me.ListBox1.Text
    Unload Me
    End Sub


    It seems like the two are not 'communicating' to each other.

    Darn, i know i am very close.

    Joe

  4. #4
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: MS Word VB Issue

    Try this:
    Code:
    Private Sub CommandButton1_Click()
        Dim i As Long
        i = Me.ListBox1.ListIndex
        If i > -1 Then
            ActiveDocument.FormFields("txtLeon").Result = Me.ListBox1.List(i)
            Unload Me
        Else
            MsgBox "Please select a list item"
        End If
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    3

    Re: MS Word VB Issue

    Nope.....still not working; same error message.

    I tell ya....i think there is somekind of manual setting that I need to fix in MS Words. It seems like the codes are not recognizing the fields or the form.

    Any suggestions?

    Thank you.

    Joe

  6. #6
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: MS Word VB Issue

    That must work if you create correct FormField textbox.
    If you use ActiveX textbox or Form textbox then the code must be different.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: MS Word VB Issue

    try replacing the reference to activedocument to the specfic document
    documents("document1").

    i tested your original code, worked fine (word 2000), whether document was protected (for forms) or not

    are you sure that txtleon is a forms textbox, not a controls textbox?
    Last edited by westconn1; Jun 21st, 2008 at 06:26 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width