Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-23-2019, 02:12 PM
Javir Javir is offline Yet another Multi-Select Listbox in Userform question Mac OS X Yet another Multi-Select Listbox in Userform question Office 2010 64bit
Novice
Yet another Multi-Select Listbox in Userform question
 
Join Date: Mar 2018
Posts: 4
Javir is on a distinguished road
Default Yet another Multi-Select Listbox in Userform question

Hi,
I've been through most of the posts related to Multi-Select Listbox in a Userform.

I have a simple letter that triggers a Userform asking for a surname and a Multi-Select Listbox (called services).

In the word doc I assigned Doc Variable var1 for the name and Doc Variable var2 to include the options chosen in the Listbox.

The problem is I can't seem to make it work.

My Ok button code is:
Private Sub cmdOK_Click()
Set oVars = ActiveDocument.Variables
Hide
oVars("var1").Value = surname.Value
ActiveDocument.Fields.Update
Set oVars = Nothing
Dim i As Long
Dim StrOut As String
For i = 0 To Me.services.ListCount - 1
If Me.services.Selected(i) Then
StrOut = StrOut & Me.services.List(i) & vbCr
End If
Next i
oVars("var3") = StrOut
Unload Me
End Sub

If I change oVars("var3") to MsgBox, the code works. But it doesn't insert the options in the document, of course, as it's a MsgBox.



Any help?

Thanks!

Javier
Reply With Quote
  #2  
Old 09-23-2019, 04:00 PM
gmaxey gmaxey is offline Yet another Multi-Select Listbox in Userform question Windows 10 Yet another Multi-Select Listbox in Userform question Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,601
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

So what happens if you:

Msgbox oVars("var3")


You are updating the fields before defining the docvariable.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 09-23-2019, 06:02 PM
Guessed's Avatar
Guessed Guessed is offline Yet another Multi-Select Listbox in Userform question Windows 10 Yet another Multi-Select Listbox in Userform question Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,164
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Why are you using oVars again after setting it to Nothing?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 09-23-2019, 08:29 PM
gmayor's Avatar
gmayor gmayor is offline Yet another Multi-Select Listbox in Userform question Windows 10 Yet another Multi-Select Listbox in Userform question Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You should also declare all your variables and update the fields when the processing is completed.

If you have fields that are not in the body of the document you will need to ensure that they are updated also.
Code:
Private Sub cmdOK_Click()
Dim i As Long
Dim StrOut As String
Dim oVars As Variables
    Set oVars = ActiveDocument.Variables
    Hide
    oVars("var1").value = Surname.value
    For i = 0 To Services.ListCount - 1
        If Services.Selected(i) Then
            StrOut = StrOut & Services.List(i) & vbCr
        End If
    Next i
    oVars("var3").value = StrOut
    UpdateAllFields
    Set oVars = Nothing
    Unload Me
End Sub

Private Sub UpdateAllFields()
Dim oStory As Range
    For Each oStory In ActiveDocument.StoryRanges
        oStory.Fields.Update
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
                Set oStory = oStory.NextStoryRange
                oStory.Fields.Update
            Wend
        End If
    Next oStory
    Set oStory = Nothing
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 09-24-2019, 01:01 AM
Javir Javir is offline Yet another Multi-Select Listbox in Userform question Windows 7 64bit Yet another Multi-Select Listbox in Userform question Office 2016
Novice
Yet another Multi-Select Listbox in Userform question
 
Join Date: Mar 2018
Posts: 4
Javir is on a distinguished road
Default

Hi everyone,

thank you for the quick reply!
Yes, @gmayor, that worked perfect.
My code was wrong from the start!

Thank you again,

javier
Reply With Quote
Reply

Tags
listbox bookmark, variables, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Userform multiselect listbox populate trevorc Excel Programming 1 12-03-2018 02:49 PM
This is a Userform LIstbox queston: A variable does not set to the value of a listbox CatMan Excel Programming 14 08-18-2014 08:14 PM
Yet another Multi-Select Listbox in Userform question This is a Userform LIstbox queston: A variable does not set to the value of a listbox CatMan Excel 1 08-08-2014 09:41 AM
How to use an ActiveX Control to insert a multi select listbox in Word marksm33 Word 2 01-29-2014 05:21 PM
Yet another Multi-Select Listbox in Userform question Multi-select listbox help gvibe@hotmail.com Word VBA 1 07-19-2013 10:54 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:29 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft