?
Solved

split formField string by carriage return

Posted on 2006-05-01
9
Medium Priority
?
334 Views
Last Modified: 2008-03-03
Hi i'm trying to split a formField string by a carriage return:-

Sub splitString()
    Dim Items As Variant
    Dim desc As String
   
    desc = ActiveDocument.FormFields("txt_description").Result
   
    Items = Split(desc, vbCrLf)
   
    For i = 0 To UBound(Items)
        MsgBox Items(i)
    Next i
End Sub

the vbCrLf is not working?  

Also i want to convert items in the array from string to a currency type?

Thanks

Ross
0
Comment
Question by:rossh1977
  • 5
  • 2
7 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16579990
Hi. I'm here too.
Try vbCr.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16580031
Currency type is rarely used in VB.

But you can do it with CCur, though it may not achive your wider objective.

Dim c As Currency
c = CCur("£1234")
 
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16580067
I guessed that you are in UK. In fact he optional leading currency sign must match your local settings, otherwise you will get a Type Mismatch error.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:rossh1977
ID: 16581297
Thanks again Graham,  I've gotten this far with this method which i'd like to get working but i'm going to look more closley at your other solution

Sub splitString()
    Dim vQuantity As Variant
    Dim vEach As Variant
    Dim txt_item As String
    Dim txt_each As String
   
    txt_quantity = ActiveDocument.FormFields("txt_quantity").Result
    txt_each = ActiveDocument.FormFields("txt_each").Result
   
    vQuantity = Split(txt_quantity, vbCr)
    vEach = Split(txt_each, vbCr)
   
    For i = 0 To UBound(vQuantity)
        ActiveDocument.FormFields("txt_total").Result = CCur(vEach(i)) * CInt(vQuantity(i)) & vbCr
    Next i
End Sub

If i print this in a msgBox it works, but using the above only enters the sum of the last each and quantity in the arrays?

Thanks

Ross
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 16583493
Yes Ross, you are overwriting the text_total result each time.

Logically you need:

    For i = 0 To UBound(vQuantity)
        ActiveDocument.FormFields("txt_total").Result = ActiveDocument.FormFields("txt_total").Result & CCur(vEach(i)) * CInt(vQuantity(i)) & vbCr
    Next i

Alternatively you could do:

    vQuantity = Split(txt_quantity, vbCr)
    vEach = Split(txt_each, vbCr)

    ReDim totals(ubound(vQuantity))
   
    For i = 0 To UBound(vQuantity)
        Totals(i) = CCur(vEach(i)) * CInt(vQuantity(i))
    Next i
    ActiveDocument.FormFields("txt_total").Result = Join(totals, vbCr)

0
 

Author Comment

by:rossh1977
ID: 16583523
Thanks again Graham works a treat, although the carriage return displays as small box, but i'll play about with it.

Thanks again excellent help!

Ross
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16603163
I'm not sure why carriage returns display as boxes, they don't for me.
They are interpreted by Word as paragraphs, causing a new line and displayed as ¶ if they are set to be visible.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question