Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Transferring listbox values in userform to document

Status
Not open for further replies.

NIA2

Technical User
Aug 30, 2006
137
AU
Hi everyone,

I have a text box in a userform in Word where the person types in some information and then clicks an "add" button to insert the entry into a listbox. They keep doing this for as many entries they need.

Each one of these listbox entries will end up being a bullet point in the Word document once the OK button is pressed in the form. I have a point in the file that's marked with a DOCVARIABLE field where I want them to go but I'm not sure how I'd go about transferring the listbox values to the marked point and then have them all appear as bullets one underneath each other and taking on the style from the initial point.

I know how to do it if I'm transferring something from a textbox, ie.

.Variables("Name").Value = txtName.Text

...but something like this for the list box isn't working:

.Variables("Personal_Interest").Value = lstPersonal_Interest.Text

Would someone be able to tell me what the code would be?

Thanks very much.
 
I only thought FillABookmark acted like a function because of the call keyword. I guess I'm used to calling functions not procedures.

Yes - confirmation that the error is:

Call FillABookmark("Personal_Interest", Left(interest, _
Len(interest) - 2))

"It is standard debugging to try and isolate actions into their own procedures. I would suggest you do this"

I'm not sure what you mean by this but I removed everything from the cmdFinish_Click() procedure and included what you suggested as below:

Code:
Private Sub cmdFinish_Click()

Application.ScreenUpdating = False

' update document variables using 
' its own procedure
Call UpDateVariables

For var = 0 To lstPersonal_Interest.ListCount - 1
    If lstPersonal_Interest.Selected(var) = True Then
        interest = interest & _
            lstPersonal_Interest.List(var) _
            & vbCrLf
     End If
Next

Call FillABookmark("Personal_Interest", Left(interest, _
    Len(interest) - 2))
End Sub

When I tested I got a compile error:

compile error: sub or function not defined

and it highlights: Call UpDateVariables

I continued to troubleshoot and I made a copy of the whole document and deleted everything from the code except that which related to the "personal interests" page. The code I used was:


Code:
Option Explicit

Private Sub MultiPage1_Change()
    cmdNext.Enabled = (MultiPage1.Value < MultiPage1.Pages.Count - 1)
    cmdPrevious.Enabled = (MultiPage1.Value > 0)
End Sub


Private Sub UserForm_Initialize()
 
MultiPage1.Value = 0

cmdChangePersonalInterest.Enabled = False
cmdDeletePersonalInterest.Enabled = False

End Sub
 


' ***** Personal Interest *****
 
Private Sub cmdChangePersonalInterest_Click()
    Dim var As Integer
    ReDim MyList(lstPersonal_Interest.ListCount - 1, 1)
    For var = 0 To lstPersonal_Interest.ListCount - 1
        MyList(var, 0) = lstPersonal_Interest.Column(0, var)
    Next
    MyList(mySelected, 0) = txtPersonal_Interest.Text
    lstPersonal_Interest.Clear
    lstPersonal_Interest.List = MyList()
    txtPersonal_Interest.Value = ""
End Sub
 
 
Private Sub lstPersonal_Interest_Change()
    mySelected = lstPersonal_Interest.ListIndex
    If mySelected > -1 Then
        txtPersonal_Interest.Text = lstPersonal_Interest.Column(0, mySelected)
        cmdChangePersonalInterest.Enabled = True
    Else
        cmdChangePersonalInterest.Enabled = False
    End If
    cmdDeletePersonalInterest.Enabled = cmdChangePersonalInterest.Enabled
 
End Sub

Private Sub cmdAddPersonalInterest_Click()
With lstPersonal_Interest
    .AddItem txtPersonal_Interest.Value
End With
txtPersonal_Interest.Value = ""


End Sub

Private Sub cmdDeletePersonalInterest_Click()
With lstPersonal_Interest
    .RemoveItem (.ListIndex)
End With
End Sub



Private Sub cmdFinish_Click()
Application.ScreenUpdating = False
'   Application.ScreenUpdating = True

Dim interest As String
Dim var


For var = 0 To lstPersonal_Interest.ListCount - 1
    If lstPersonal_Interest.Selected(var) = True Then
        interest = interest & lstPersonal_Interest.List(var) _
        & vbCrLf
End If
Next

Call FillABookmark("Personal_Interest", Left(interest, _
    Len(interest) - 2))



With ActiveDocument
    .PrintPreview
    .ClosePrintPreview
End With
        Unload Me

End Sub


Private Sub cmdCancel_Click()

If MsgBox("Are you sure you want to Cancel? You will lose all changes.", vbYesNo + vbQuestion) = vbYes Then
    Set frmEnergeticsCV = Nothing
    Unload Me
End If

End Sub

Still coming up with the same error.

One bit of code I forgot to mention was that I have the following in another separate module.

Code:
Option Explicit

Public MyList() As String

It relates to this code:

Code:
Private Sub cmdChangePersonalInterest_Click()
    Dim var As Integer
    ReDim MyList(lstPersonal_Interest.ListCount - 1, 1)
    For var = 0 To lstPersonal_Interest.ListCount - 1
        MyList(var, 0) = lstPersonal_Interest.Column(0, var)
    Next
    MyList(mySelected, 0) = txtPersonal_Interest.Text
    lstPersonal_Interest.Clear
    lstPersonal_Interest.List = MyList()
    txtPersonal_Interest.Value = ""
End Sub

Just thought I'd mention this incase it reveals something.

So I guess I've covered everything - I'm as confused as ever, especially since you're saying it's working on your side.

Just one thing - the modules that contain the two separate code blocks are just called "module1" and "module2". Do these need to be called anything unique? Thought this may have something to do with it.


 
By the way, if you've run out of suggestions is there a way I can get a hold of your test document, ie. maybe via another forum that offers file uploads?
 
You can contact me:

myhandle at telus dot net (home); OR

gerry dot knight at servicecanada dot gc dot ca (work)

where I am heading off to shortly. I should be there in 45 minutes. My sample test file is there. if you contact me there, I can send that test file to you. Or even better, you can send me your test file and I will look at it.

You are getting an error re: Call UpdateVariables because you do not have a sub UpdateVariables most likely. I tossed that in there to make the point that good programming isolates chunks into their own procedures. So you write a procedures that does the Variables stuff, and ONLY the Variables stuff, and then you call it. The reason being is any errors will be isolated into explicit procedures. Something like:
Code:
Sub UpdateVariables()
With ActiveDocument
        .Variables("Name").Value = txtName.Text
        .Variables("Title").Value = txtTitle.Text
        .Variables("Position").Value = txtPosition.Text
        .Variables("Summary_of_Experience").Value = txtSummary_of_Experience.Text
        .Variables("Month1").Value = cboMonth1.Value
        .Variables("Year1").Value = cboYear1.Value
        .Variables("State1").Value = cboState1.Value
        .Variables("Company1").Value = cboCompany1.Value
        .Variables("Month2").Value = cboMonth2.Value
        .Variables("Year2").Value = cboYear2.Value
        .Variables("Company2").Value = txtCompany2.Text
        .Variables("State2").Value = cboState2.Value
        .Variables("Month3").Value = cboMonth3.Value
        .Variables("Year3").Value = cboYear3.Value
        .Variables("Company3").Value = txtCompany3.Text
        .Variables("State3").Value = cboState3.Value
        .Variables("Role1").Value = txtRole1.Text
        .Variables("Job_Summary1").Value = txtJob_Summary1.Text
        .Variables("Role2").Value = txtRole2.Text
        .Variables("Job_Summary2").Value = txtJob_Summary2.Text
        .Variables("Role3").Value = txtRole3.Text
        .Variables("Job_Summary3").Value = txtJob_Summary3.Text

End With
End Sub

In other words, if there is a problem with the chunk of code updating the Variables, the error will point to THERE. The more you have code that does many different actions - but all in one procedure - the harder it is to narrow down problems. By making chunks their own procedures errors will be also differentiated to those procedures.

faq219-2884

Gerry
My paintings and sculpture
 
Update: OP sent me file but I can not open it as it is .dotm and I do not have 2007, nor will I install the Compatability Pack.

Actually the file is: CVInProgress.dot.dotm

Is this normal with 2007 files, having two extensions like that?

In any case, I sent the OP my testing file.

faq219-2884

Gerry
My paintings and sculpture
 
Thanks for sending the file. Clearly yours does work but unfortunately it doesn't help me find a solution to the problem in my own file.

I'm actually using Parallels on the mac, and have Windows Vista and Word 2007 installed. The file CVInProgress does appear as CVInProgress.dot on my system but for some reason when I email it it adds the .dotm extension as well. When I drag the file out of Outlook and back onto my desktop the .dotm extension disappears and the .dot only remains. Did you try and do this?

I also saved the file for backward compatibility.

I know this post is getting a bit long but I really wanted to find a solution. I guess if you're unable to open the file then I'll wrap up the post.

Thanks for your continued support.
 
Let's not give up quite yet.

1. where is mySelected declared?
Code:
MyList([highlight]mySelected[/highlight], 0) = txtPersonal_Interest.Text

I can not see it.

2. If you would, copy out ALL, and I mean ALL, of the code from the file. Make sure you identify what module it belongs to. Send that to me as text. I want to actually look at what you have.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top