how to parse a string using VBA?

G

Geoff Cox

Hello,

I have

string = "M 0.006 0.005 L 0.4 0.3"

and wish to get at the numbers

0.006, 0.005, 0.4 and 0.3

but cannot find a way of getting them using the space character as the
delimiter.

Any suggestions please?

Cheers

Geoff

PS this relates to PowerPoint - and you may guess that I am trying to
get the values for FromX, FromY, ToX and ToY from the path data for a
motion effect - is there an easier way?!
 
D

David M. Marcovitz

I'm sure there are easier wasy, but this does what you want:

Dim myString As String
Dim aStrings(6) As String


Sub ParseMyString(sString As String)
Dim iLength, i, count, newStart As Long
For i = 0 To 5
aStrings(i) = ""
Next i
iLength = Len(sString)
count = 0
For i = 1 To iLength
If Mid(sString, i, 1) = " " Then
count = count + 1
newStart = i + 1
Else
aStrings(count) = aStrings(count) & Mid(sString, i, 1)
End If
Next i
End Sub
Sub GiveMeParsedString()
ParseMyString (InputBox("Type a string in four parts separated by one
space"))
GiveMeResults
End Sub
Sub GiveMeResults()
MsgBox aStrings(0)
MsgBox aStrings(1)
MsgBox aStrings(2)
MsgBox aStrings(3)
MsgBox aStrings(4)
MsgBox aStrings(5)
End Sub


--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
 
G

Geoff Cox

I'm sure there are easier wasy, but this does what you want:

David,

Thanks for the code - any idea what the easier way might be?!

Is it possible to get the values of FromX, FromY, ToX and ToY
directly?

Cheers

Geoff
 
D

David M. Marcovitz

If I had ideas about the easier ways, I would have given them. I really
don't know.
--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
 
K

Karl E. Peterson

Geoff said:
Thanks for the code - any idea what the easier way might be?!

Is it possible to get the values of FromX, FromY, ToX and ToY
directly?

s = "M 0.006 0.005 L 0.4 0.3"
FromX = Split(s)(1)
FromY = Split(s)(2)
ToX = Split(s)(4)
ToY = Split(s)(5)

Later... Karl
 
G

Geoff Cox

s = "M 0.006 0.005 L 0.4 0.3"
FromX = Split(s)(1)
FromY = Split(s)(2)
ToX = Split(s)(4)
ToY = Split(s)(5)

Thanks for the above Karl!

Can you tell me where the Split function is mentioned on MSDN, or
anywhere else for that matter - I had tried and failed to find it. Not
in the Mastering VBA (2nd ed) by Guy Hart_Davis etc.

Cheers

Geoff
 
K

Karl E. Peterson

Geoff said:
Thanks for the above Karl!

Can you tell me where the Split function is mentioned on MSDN, or
anywhere else for that matter - I had tried and failed to find it. Not
in the Mastering VBA (2nd ed) by Guy Hart_Davis etc.

Put your cursor on it -- the word "Split" itself -- and press F1. :)
 
S

Shyam Pillai

Geoff,
FromX, FromY, ToX, ToY are distinct from the Path property. Use either the
Path property to assign the motion path using VML string or make using of
FromX, FromY,ByX,ByY and ToX and ToY properties.


--
Regards,
Shyam Pillai

Image Importer Wizard
http://skp.mvps.org/iiw.htm
 
S

Steve Rindsberg

Put your cursor on it -- the word "Split" itself -- and press F1. :)

Help's often so badly hosed in PPT2002/2003 that this doesn't work.

You have to type the term into the "Beg for help here" box in u/r corner of
IDE.
 
G

Geoff Cox

Put your cursor on it -- the word "Split" itself -- and press F1. :)

Karl,

Never had this work with PPT 2003!

When I look up Spilt and then Split function with MS Visual Basic
HelpI don't seem to get the version you used - do I?

Cheers

Geoff
 
G

Geoff Cox

Geoff,
FromX, FromY, ToX, ToY are distinct from the Path property. Use either the
Path property to assign the motion path using VML string or make using of
FromX, FromY,ByX,ByY and ToX and ToY properties.

Shyam,

You may remember that I have been trying to deal with words which move
across the slide from one column to another and changing them from On
Click to Click on shape so that I can then use the kiosk mode.

The code below seems to work OK, but it may well lack elegance (!) and
perhaps you could explain this better way to get the FromX etc values?

Thanks

Geoff

effectId:=msoAnimEffectPathCurvyStar, _

Sub check_for_button(strMyFile As String)

Dim oPresentation As Presentation
Set oPresentation = Presentations.Open(strMyFile)
Dim oSl As Slide
Dim y As Long
Dim i As Long
Dim oSh As Shape
Dim oEffect As Effect
Dim oShB As Shape
Dim pathdata(20)
Dim aniMotion As AnimationBehavior

With oPresentation

For Each oSl In ActivePresentation.Slides

ActiveWindow.View.GotoSlide (oSl.SlideIndex)

With oSl.TimeLine

For i = .MainSequence.count To 1 Step -1

If .MainSequence(i).Shape.Type = msoTextBox Then
Set shpAnswer = oSl.Shapes.AddShape(msoShapeRectangle, _
600, 50, 100, 50)
shpAnswer.TextFrame.TextRange.Text = "Answer"
GoTo Nextsub
End If
Next

End With

Nextsub: With oSl.TimeLine
y = 1
For i = .MainSequence.count To 1 Step -1

If .MainSequence(i).Shape.Type = msoTextBox Then
Set oSh = .MainSequence(i).Shape
pathdata(y) = .MainSequence(i).Behaviors(1).MotionEffect.path

Set oEffect = _
oSl.TimeLine.InteractiveSequences.Add.AddEffect(Shape:=oSh, _
effectId:=msoAnimEffectCustom, _ Trigger:=msoAnimTriggerOnShapeClick)

With oEffect.Timing
..Duration = 2
..TriggerDelayTime = 0
End With

Set aniMotion = oEffect.Behaviors.Add(msoAnimTypeMotion)

With aniMotion.MotionEffect
.fromx = Split(pathdata(y))(1)
.FromY = Split(pathdata(y))(2)
.ToX = Split(pathdata(y))(4)
.ToY = Split(pathdata(y))(5)

End With

oEffect.Timing.TriggerShape = shpAnswer


End If

y = y + 1
Next i

End With

Next oSl

oPresentation.Save
oPresentation.Close

End With

Set oSh = Nothing
Set oPresentation = Nothing
End Sub
 
D

David M. Marcovitz

Thought you might be interested to know that array you declared
actually has 7 elements, indexed 0 to 6. Fwiw...

Thanks. I always forget that the 6 refers to the upper bound not the number
of elements. I usually just ignore 0 because most things I want to count
(and my non-programming students) want to count start at 1.
 
D

dolivastro

Also, does this work the way it is (I think) intended to work?

Dim iLength, i, count, newStart As Long

The datatypes of iLength, i, count, and newStart are (I think) supposed
to be long. But are they?

Dom
 
S

Steve Rindsberg

John Wilson said:
Works for me Steve! (2003 sp2)

That's the way it's supposed to work. As long as your luck's holding, hold
your breath and don't change anyhing and keep your tinfoil hat firmly in place.
<g>

It doesn't work on any of my 2003 installs. It may have something to do with
multiple PPT versions on the same box, but I'm not certain.
 
G

Geoff Cox

That's the way it's supposed to work. As long as your luck's holding, hold
your breath and don't change anyhing and keep your tinfoil hat firmly in place.
<g>

It doesn't work on any of my 2003 installs. It may have something to do with
multiple PPT versions on the same box, but I'm not certain.

Steve,

I have only the 2003 version (with SP 2) and F1 does not work here
either.

Geoff
 
G

Geoff Cox

Hi Geoff,
All that you are doing is, copying the motion path. You don't need Split if
you use:

aniMotion.MotionEffect.Path= PathData

Thanks Shyam - much neater.

Cheers

Geoff
 
K

Karl E. Peterson

Steve said:
Help's often so badly hosed in PPT2002/2003 that this doesn't work.

You have to type the term into the "Beg for help here" box in u/r
corner of IDE.

Yeah, good point. Although I'd add it's *not* just PPT or even the 02/03
versions! I have troubles with O2K help at times. No idea what causes it,
or what I do to "solve" it when it does work.
 

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