PDA

View Full Version : Solved: Extract a particular Text within some specified delimiters



sidney_jec
07-03-2008, 05:12 AM
Hi
I am having problem with the word macros.
I am having a string of the format WPAB_WP4_EN_GB_ which i get into a variable. Now what i want is to extract the string between the first underscore and the second underscore OR the 2nd underscore and 3rd underscore etc.
I can not hardcode it as i am not aware of the value at compile time as it is computed at run time and may vary like CRTB_DIT5_NL_NL_.
Kindly suggest ASAP.

PS: m sorry if the topic already exists..i didn't have the patience to look for it in such a vast database. Mods pls feel free to merge the question with an existing topic (if at all, but pls inform me abt it :) )


Thanks in advance..

CreganTur
07-03-2008, 06:52 AM
This may not be the most optimal way to go about it, but the Sub below will pull apart the string you posted above into 4 different variables.

Sub ParseTest()
Dim strTest As String
Dim strOne As String
Dim strTwo As String
Dim strThree As String
Dim strFour As String

strTest = "WPAB_WP4_EN_GB_" '<<<Original String we're evaluating

strOne = Left(strTest, InStr(1, strTest, "_", vbTextCompare)) '<<<get first part of String
strTest = Right(strTest, Len(strTest) - InStr(1, strTest, "_", vbTextCompare)) '<<<Truncate original string
strTwo = Left(strTest, InStr(1, strTest, "_", vbTextCompare)) '<<<get second part of string
strTest = Right(strTest, Len(strTest) - InStr(1, strTest, "_", vbTextCompare)) '<<<truncate string
strThree = Left(strTest, InStr(1, strTest, "_", vbTextCompare)) '<<<get third part of string
strTest = Right(strTest, Len(strTest) - InStr(1, strTest, "_", vbTextCompare)) '<<<truncate string again
strFour = strTest '<<<Remaining portion of string assigned to variable

MsgBox "String has been broken apart." & vbCrLf & vbCrLf & strOne _
& vbCrLf & strTwo & vbCrLf & strThree & vbCrLf & strFour
End Sub

Just C&P it into a module, put your cursor somewhere inside the sub, and pres F5 to see it run.

HTH:thumb

sidney_jec
07-03-2008, 06:58 AM
thanks man..
will try this out tomorrow and let u know in case of any queries..

Tommy
07-03-2008, 08:12 AM
just another way to do the same thing:

Sub SplitOnUnderscore()
Dim HldStr() As String
Dim strTest As String
strTest = "WPAB_WP4_EN_GB_"
HldStr = Split(strTest, "_")
If Right(strTest, 1) = "_" Then
ReDim Preserve HldStr(UBound(HldStr, 1) - 1)
End If
MsgBox Join(HldStr, vbCrLf)
End Sub

MOS MASTER
07-03-2008, 01:57 PM
Hi & Welcome to VBAX Sidney, :hi:

Nice one Tommy an Array makes this very flexible to process! :yes

sidney_jec
07-03-2008, 10:19 PM
thanks MOS..
and thanks to Tommy and CreganTur as well..
this has solved my problem without any hassles..

sidney_jec
07-04-2008, 04:20 AM
just another way to do the same thing:

Sub SplitOnUnderscore()
Dim HldStr() As String
Dim strTest As String
strTest = "WPAB_WP4_EN_GB_"
HldStr = Split(strTest, "_")
If Right(strTest, 1) = "_" Then
ReDim Preserve HldStr(UBound(HldStr, 1) - 1)
End If
MsgBox Join(HldStr, vbCrLf)
End Sub


Hi
Though the codes works wonderfully it wont run in Word 97 as Split function's not avlbl in 97 and was introduced in Word 2000 (sadly i have to put the code in 97 :(.. anws the code by CreganTur was equally helpful)