Manipulating text through VBA

A

Angie M.

Hello, I have a macro that starts with the following plain text:

C:\databasel\bus_re\imanage\admin\19504_1.doc

and ends up with this:

bus_re\19245_2

I've been able to make it happen through a series of painful moves and
deletions. My question is, what is the correct approach when writing code,
to eliminate portions of text as in my example? Thank you for any
suggestions.
 
G

Greg Maxey

As this answers should show, you are going to have to provide a little
more detail:

Sub ModifyString()
Dim myStr As String
myStr = "C:\databasel\bus_re\imanage\admin\19504_1.doc"
MsgBox "Macro starts with: " & myStr
myStr = "bus_re\19245_2"
MsgBox "Macro ends with: " & myStr
End Sub
 
J

Jean-Guy Marcil

Angie M. was telling us:
Angie M. nous racontait que :
Hello, I have a macro that starts with the following plain text:

C:\databasel\bus_re\imanage\admin\19504_1.doc

and ends up with this:

bus_re\19245_2

I've been able to make it happen through a series of painful moves and
deletions. My question is, what is the correct approach when writing
code, to eliminate portions of text as in my example? Thank you for
any suggestions.

I think you want to know how to transform the first text into the second....

The problem is that we do not know where "245_2" comes from.
Nor do we know where the initial string comes from.
Finally, we do not know what you intend to do with the final string.
Not withstanding all this, here is something that should get you going.
(Next time, it would be more helpful for those who are trying to help if you
provided more details regarding your intentions, expectations and context)

If it was a typo and you meant to transform the first text into
bus_re\19504_1

Then, here is how you could do that:

'_______________________________________
Sub MyTest()

'Initial string as selected by user in document
Dim myString As String
'Final string to overwrite initial one
Dim myFinalString As String
'Temporary array to store strings delimited by \ in initial string
Dim myBunchOfStrings1() As String
'Temporary array to store strings delimited by . in final string
Dim myBunchOfStrings2() As String
'Counter
Dim i As Long
'Target folder
Const subString As String = "bus_re"

'Initialize string
myString = Selection.Text

'Separate string in array
'In your example we will finish with
'myBunchOfStrings(0) = C:
'myBunchOfStrings(1) = databasel
'myBunchOfStrings(2) = bus_re
'myBunchOfStrings(3) = imanage
'myBunchOfStrings(4) = admin
'myBunchOfStrings(5) = 19504_1.doc
myBunchOfStrings = Split(myString, "\")
'Cycle each sub string until we find the target folder
'Here I assume you want "bus_re" and that it could be anywhere in the path
'If you always wanted the third subfolder, the code would be different
For i = 0 To UBound(myBunchOfStrings)
If myBunchOfStrings(i) = subString Then
'When found, build final string
myFinalString = subString & "\" _
& myBunchOfStrings(UBound(myBunchOfStrings))
'Remove the ".doc" part
myFinalString = Left(myFinalString, Len(myFinalString) - 4)
'Here is a different way of doing the same as above
'(to remove the ".doc" part), in case user does not select
'to the end of the path, of course, if the path came from a
'reliable source, then it would not be needed, also, this second
'Approach created problems if the document as a "." somewher in
'its name, as in : "Summary.19504_1.doc"
' myBunchOfStrings2 = Split(myFinalString, ".")
' myFinalString = myBunchOfStrings2(0)

'Insert final string in document to replace initial text
Selection.Text = myFinalString
'Exit sub as job is finished
Exit Sub
End If
Next

'If we get to this point, there was a problem, probably that the
'target folder was not found above
MsgBox "The folder "" & subString & "" was not found.", _
vbExclamation, "Cancelled"

End Sub
'_______________________________________

Manipulating string can get hairy, especially if the user provides it by
typing or selecting text. Then you are guaranteed that it will eventually
failed due to human error. You have to code to trap errors.

Also, note that the above will not work with Office 97, Split was not
available then.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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