Link to home
Start Free TrialLog in
Avatar of rossh1977
rossh1977

asked on

split formField string by carriage return

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
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi. I'm here too.
Try vbCr.
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")
 
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.
Avatar of rossh1977
rossh1977

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.