473,396 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Listbox Strings to word

Hi, Im using alistbox to insert names in a word document, im' using the following code but it only inserts the first ITEM on the list.


Code:
Expand|Select|Wrap|Line Numbers
  1. Dim strList1 As String
  2. strList1 = ListBox1.List(ListBox1.ListIndex)
  3. ActiveDocument.Bookmarks("p91").Range.Text = strList1
My question is: How can i do to insert ALL items in the listbox in word. I need them to be separated by comas and the last one by "and". Ex:

ITEM1, ITEM2, ITEM3 and ITEM4.

ITEM1, ITEM2 and ITEM3.

ITEM1, ITEM2 and ITEM5.


Of course, if theres only 1 item i need the "and" to be deleted. Ex:

ITEM1

THANK YOU VERY VEERY MUCH!!
Sep 7 '09 #1
6 2499
Frinavale
9,735 Expert Mod 8TB
Are you inserting all of the the items every time or just selected items?

Regardless, you need to loop through the ListBoxes's Items.

I'm not sure if you're using VB.NET or VB6...but here's what you'd do in VB.NET:

Expand|Select|Wrap|Line Numbers
  1. Dim str As New StringBuilder()
  2.  
  3. For i As Integer =0 To ListBox1.Items.Count - 1
  4.   Dim li = ListBox1.Items(i)
  5.   If str.Length > 0 And i < ListBox1.Items.Count - 2 Then
  6.     str.Append(", ")
  7.   Else If i = ListBox1.Items.Count - 2 Then
  8.     str.Append("And ")
  9.   End If
  10.   str.Append(li.ToString())
  11. Next
  12.  
Sep 9 '09 #2
@Frinavale


HI, THANKS FOR THE REPLY
Here's what i have
Expand|Select|Wrap|Line Numbers
  1. Dim str1 As String
  2.    Dim i As Integer
  3.    Dim s As String
  4.  
  5.    With ListBox1
  6.        For i = 0 To .ListCount - 1
  7.            str1 = str1 & s & .List(i)
  8.            s = ", "
  9.            If i = .ListCount - 2 Then s = " y "
  10.        Next i
  11.  
  12.        Debug.Print str1
  13.    End With
  14. ActiveDocument.Bookmarks("p91").Range.Text = str1
It works OK, The thing is i want to insert only the selected items (im using a listbox with checkboxes). Please Help Me...
Sep 9 '09 #3
Frinavale
9,735 Expert Mod 8TB
Then loop through the SelectedItems instead... what language are you using? VB6 or VB.NET?
Sep 10 '09 #4
@Frinavale

Thaks, im using vb6, i need to insert only items that are checked. Could you please send me the code? thanks
Sep 10 '09 #5
Frinavale
9,735 Expert Mod 8TB
I'm sorry, you had posted your question in the "introductions" forum and I wasn't sure what language you were using so my advise so far to you is probably not going to work. I've moved your question to the VB6 where it's more likely that you'll actually get help with the problem.

Happy coding!

-Frinny
Sep 10 '09 #6
kadghar
1,295 Expert 1GB
@esperanto234
Yes sir!! Ô_o


Just write an IF inside the loop

Expand|Select|Wrap|Line Numbers
  1. For i = 0 to MyList.ListCount-1
  2. If MyList.Selected(i) then 'Your code for writing it somewhere
  3. Next
HTH
Sep 21 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: amber | last post by:
Hello. Can someone tell me what I may be doing wrong here? I'm using the code (lboxRP is a listbox): Dim newRPindex As Integer newRPindex = Me.lboxRP.FindString(RP)...
1
by: Ron James | last post by:
I have a Listbox containing strings. When calling Contains (), I would like a case insensitive comparison. (I don't want to add newfile.txt to a Listbox containing NewFile.txt). I'm...
5
by: pnp | last post by:
to store other items that just strings? Thanks Peter
1
by: Richard Bond | last post by:
Hi, A colleague of mine has added a 60,000 array of strings to a listbox using the addrange method. He did this because it seemed the quickest method of getting 60,000 items into the listbox. ...
89
by: scroopy | last post by:
Hi, I've always used std::string but I'm having to use a 3rd party library that returns const char*s. Given: char* pString1 = "Blah "; const char* pString2 = "Blah Blah"; How do I append...
14
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
8
by: nirdeshonline | last post by:
Hi, I have added a simple listbox in windows form under c# 2.0. It contains a collection of approx 10 strings as list items. Now when i resize the form whole listbox flickers. Please tell me...
0
by: Stefan P. | last post by:
Hi everyody, I want to diplay strings of some objects in a LixtBox. Sometimes the text is longer than my ListBox and a horizontal scrollbar is displayed. Is it possible to get the ListBox to...
8
by: jh | last post by:
I'd like to copy/paste into a listbox during runtime. I can do this for a textbox but can't figure out how to accomplish this for a listbox. Any help? Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.