String Manipulation Query

  • Thread starter spunkymuffmonkey
  • Start date
S

spunkymuffmonkey

Hi,

I’m looking for a little help with string manipulation.

We use forms which detail daily events with our clients, each document has
the client’s name and client ID number at the top of the document for easy
recognition of whom the document relates to.

In the newer versions of these forms the client name and ID are entered into
a table, which allows me to easily pass this data into a variable.

However, in older versions there is no table, the client’s name and ID are
entered onto the form, but just as ‘free standing’ text, they normally appear
like:

Name of Client: Amy Client CareFirst ID: 19024481

I am having difficulty thinking how I can create a reliable variable when I
cannot tell where the name might start/end etc. I would need to pass the
name and the ID (19024481) into a variable so that I can use them for other
things.

It’s normally a tab that separates the two bits of info. Please let me know
if anymore information is required and thank you for any suggestions anybody
may have.

Kind regards
 
F

Fumei2 via OfficeKB.com

Say you have:

Name of Client: Amy Client CareFirst ID: 19024481
Name of Client: Any One CareFirst ID: 34227781
Name of Client: Joe Blow CareFirst ID: 44510009
Name of Client: Someone Else CareFirst ID: 71123446

then:

Sub ClientID_1()
Dim r As Range
Dim strName As String
Dim strID As String

Set r = ActiveDocument.Paragraphs(3).Range

With r.Find
.Text = "Name of Client:"
.Execute
With r
.Collapse 0
.Expand Unit:=wdParagraph
strName = r.Words(5) & r.Words(6)
strID = r.Words(r.Words.Count - 1)
End With
End With
MsgBox strName & vbTab & strID
End Sub

returns (from paragraph 3) - Joe Blow 44510009

While:

Sub ClientID_2()
Dim r As Range
Dim strList As String

Set r = ActiveDocument.Range

With r.Find
.ClearFormatting
Do While .Execute(FindText:="Name of Client:", _
Forward:=True) = True
With r
.Collapse 0
.Expand Unit:=wdParagraph
strList = strList & r.Words(5) & r.Words(6) & _
vbTab & r.Words(r.Words.Count - 1) & _
vbCrLf
.Collapse 0
End With
Loop
End With
MsgBox strList
End Sub

returns:

Amy Client 19024481
Any One 34227781
Joe Blow 44510009
Someone Else 71123446

It all depends on what EXACTLY you want to get from your string manipulation
(and also assuming each line is terminated by a paragraph mark.

In any case, undoubtably you can in fact do whatevr string manipulation you
want.

Hope this helps.


Note regarding Word VBA and "words".

Name of Client: Amy Client CareFirst ID: 19024481

"word" 1 = Name
"word" 2 = of
"word" 3 = Client
"word" 4 = :
"word" 5 = Amy
"word" 6 = Client
"word" 7 = CareFirst
"word" 8 = ID
"word" 9 = :
"word" 10 = 19024481
"word" 11 = ^p (the paragraph mark)

Thus "Amy Client" = word(5) & word(6) - Amy is not word(4)!; and "19024481"
is
r.words.count - 1. The paragraph mark is the last word.

Further, if "19024481" was followed by a period and a paragraph mark, then it
would be
r.words.count - 2. The period at the end is a "word".
 
S

spunkymuffmonkey

A million thank you's for your kind assistance, this is exactly what I needed
and will be very useful to me.

One more thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top