+ Reply to Thread
Results 1 to 19 of 19

Populate word document with listbox selections

  1. #1
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Populate word document with listbox selections

    I have created a listbox in word which is populated via selections from a number of other listboxes.

    How can I populate a word document with the selections found in this listbox. This will be assigned to a button click which closes the userform?

    many thanks,

    Andy

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Populate word document with listbox selections

    Hi Andy,

    Depending on what you want to do with the data, you could, for example, write to a table or to bookmarks in the document or to custom document properties that are referenced via DOCPROPERTY fields.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Populate word document with listbox selections

    Thanks Paul

    I simply want to "paste" the data into a word document, which will form part of a larger document.

    I have dispensed with bookmarks as I have elected to insert the data at the current cursor position.

    I am not familiar with DOCPROPERTIES. I am going to have to do some googling again

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Populate word document with listbox selections

    Inserting the data at the current cursor position is
    • OK - until you want to change it;
    • not terribly useful when you want to insert data at multiple locations.

  5. #5
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Populate word document with listbox selections

    The data won't be changed once it's in (I am in the process of building in some code to carry out formatting etc. prior to it being submitted), and the insertion points are fixed.

    Not having much luck with google though. It seems unless you've got a wealth of experience already it can be a little hard to work out how to adapt certain codes to work in word, let alone read through it and understand what it's doing..

  6. #6
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Populate word document with listbox selections

    All this makes me wonder why you're using a userform rather than, say content controls or formfields.

    As for programmatically creating & using custom document properties, see: http://support.microsoft.com/kb/212618/en-us

    A simple demo of populating a custom document property from a userform and updating the document is attached. The custom document property is named 'Demo'. The corresponding DOCPROPERTY field could be positioned/repilcated as often as need be anywhere in the body of the document.
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Populate word document with listbox selections

    Thanks for the response. I already have something similar to what you've shown in the attached example. However, I think I may have not explained myself correctly...What I'm looking for is as follows by way of an example.

    Listbox1 (with multiple selection enabled)
    Name A
    Name B
    Name C
    Name D etc.

    Listbox2 (with multiple selection enabled)
    Name 1
    Name 2
    Name 3
    Name 4 etc.

    Listbox3 contains the selected entries from the previous two. The data shown in this one is what I want pasted into the word document on the cmdbutton click event.

    Hope this makes sense,

    Andy

  8. #8
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Populate word document with listbox selections

    Hi Andy,

    I understood well enough what you meant. What I was demonstrating was the population of a custom document property and, from that, the document. The same principles can be applied to your Listbox3. With this approach, you don't need to know (in a programmatic sense) where the output will end up.

  9. #9
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Populate word document with listbox selections

    Then I guess it is I that really doesn't understand. Thank you though.

  10. #10
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Populate word document with listbox selections

    Can this not be performed assigned to a click event so that the data shown appears as a string in the document, at the cursor point.

  11. #11
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Populate word document with listbox selections

    Yes, of course it can, but you really shouldn't be wedded to a dependency on what just happens to be selected at the time. The result could be data inserted in the wrong location and other content being erased.
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Populate word document with listbox selections

    Ok, I see what you've done (apart from the Docproperty stuff!). That is pretty much what I'm after...give me a mo and I'll send through an example...

  13. #13
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Populate word document with listbox selections

    There's no 'Docproperty stuff' in the last example - it simply (over)writes whatever happens to be selected at the time.

  14. #14
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Populate word document with listbox selections

    I used the term 'stuff' simply because as you're no doubt aware I know about as much on this a a 3yr old child could write on a postage stamp...

    I'll send you an example and you can have a good laugh at how amateurish and possibly pointless my query is.

  15. #15
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Populate word document with listbox selections

    Here is an example of what I was talking about...

    I just can't get the selected entries to copy into the document underneath the entry you see when you click 'OK'.
    Attached Files Attached Files

  16. #16
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Populate word document with listbox selections

    Try something like:
    Please Login or Register  to view this content.

  17. #17
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Populate word document with listbox selections

    Thank you works a treat. Only one question though. If I change the names to firstname lastname it puts a comma between them. I've tried but I either get rid of the comma altogether or it appears between everything...

  18. #18
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Populate word document with listbox selections

    In that case, you might use something like:
    StrTeam = StrTeam & lbUsed.List(i, 0) & "|"
    ...
    Replace(Left(StrTeam, Len(StrTeam) - 1), "|", ", ") & "."

  19. #19
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Populate word document with listbox selections

    Yet again you've come up trumps. I've modified it a bit as I think I want the names to appear on separate lines, but THANK YOU!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1