PDA

View Full Version : [SOLVED:] Select Case Statements - Many in a Block Set



dj44
10-09-2017, 02:59 PM
folks good day

:)


I have got stuck on my select case.

Well i know there are other ways of doing what im trying to do with arrays and other stuff.

But i always get stuck on using select case although its meant to be easier

I wanted to keep my select case statements above in a block

Then I have my main code

So it helps me to keep things separate.

well i set it up but well it doesnt seem to be happening


this is a basic example.





Sub Select_Case()



For Each oPara In ActiveDocument.Paragraphs

Set oRng = oPara.Range

oFindText = oPara.Range.Text


'-------- All My Select Cases
Select Case oFindText

Case "@@@"
oInsertBefore = "Hello"

Case "***"
oInsertBefore = "Apple"

Case "###"
oInsertBefore = "Pear"

End Select
'-----------------------------





'-------- Main Code Block

If InStr(1, oRng.Text, oFindText) > 0 Then

oRng.Characters.First.InsertBefore oInsertBefore


End If
Next oPara


End Sub




I m not good at writing functions, and i was trying to avoid arrays

my Select case should work but im not sure why my idea doesnt work - it doesnt minus some syntaxtical error i suppose.

SamT
10-09-2017, 04:07 PM
Are those three bits whole words?

A paragrapgh that consists soley of one string, "@@@" doesn't sound reasonable

dj44
10-09-2017, 04:23 PM
Hello Sam,

:)

Well theres the first mistake.

Yes, I was looking for any paragraphs that contain "@@@" then i will insert "Hello" before the paragraph

I mean thats the idea of it.

But something has gone wrong in my select cases and Im not sure what else.

I know how to do basic select cases, but i wanted to have a block set of statements to keep things organised.
Im a bit old school i need my things laid out easily other wise i usually make a mess as is beknown

macropod
10-09-2017, 08:30 PM
The basic problem you have is that oPara.Range includes a paragraph break you've ignored.


Sub Select_Case()
For Each oPara In ActiveDocument.Paragraphs
oInsertBefore = "": Set oRng = oPara.Range
oFindText = Split(oPara.Range.Text, vbCr)(0)
Select Case oFindText
Case "@@@"
oInsertBefore = "Hello"
Case "***"
oInsertBefore = "Apple"
Case "###"
oInsertBefore = "Pear"
End Select
If Len(oInsertBefore) > 0 Then oRng.InsertBefore oInsertBefore
Next oPara
End Sub

dj44
10-10-2017, 06:59 AM
Hello Paul,

thank you for this, yes its the right idea, now i ran it on the sample text below

=========================

Lorem Ipsum is simply dummy text of the printing and typesetting industry.

@@@ Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,

### when an unknown printer took a galley of type and

*** scrambled it to make a type specimen book. It has survived not only five centuries,

========================

but no text was inserted and i tried alot of experimentation im not sure if its me?

mdmackillop
10-10-2017, 07:51 AM
If you're looking for the first 3 characters use Select Case Left(oFindText, 3)
Otherwise try

Sub LoopArray()
arr1 = Array("@@@", "***", "###")
arr2 = Array("Hello", "Apple", "Pear")
For Each oPara In ActiveDocument.Paragraphs
oInsertBefore = "": Set oRng = oPara.Range
For i = 0 To 2
If InStr(1, oPara.Range.Text, arr1(i)) Then oInsertBefore = arr2(i)
Next i
If Len(oInsertBefore) > 0 Then oRng.InsertBefore oInsertBefore
Next oPara
End Sub

dj44
10-10-2017, 08:24 AM
Hello M nice to see you,


Select Case Left(oFindText, 3)


it seems to do the trick.

Well with that and having to split paragraphs it's no wonder the select case got all so confusing when it was meant to be the simpleton.

But anyway thank you for the additional array.

The problem with arrays - I always delete something and forget to delete from the other array - and then have two sets of arrays that get very messed up like a soup.

i thought id better lay things out simply with a select case :)

Thanks for the help

Have a good day M and Paul and everyone.