?
Solved

Help writing an IF statement to find string and then filter against my array of words

Posted on 2013-12-07
5
Medium Priority
?
253 Views
Last Modified: 2013-12-10
Hi experts

I want to see if a paragraph style name contains the word "bullet" or "Bullet" , if it does then check it isn't one of the strings in my array of strings "astrWords".

Or another way to say that would be...
if style name contains the word “bullet” or “Bullet” anywhere in the string, but is NOT one of the strings in my array of words "astrWords" then do what I need to do.

Can someone please help. I have no idea how to do it.

 
Sub BulletReplacement ()

    Dim para As Paragraph
    Dim astrWords()  As String
   
'Return array containing paras not to search
   astrWords = getRecognisedBulletedStyleNames

    For Each para In activedocument.Paragraphs
	‘HELP HERE if style name contains the word “bullet” or “Bullet” but is NOT one of the astrWords
        If InStr(para.Style, "bullet") <> 0 Then
                para.Style = "List Bullet"
        End If
    Next para

End Sub


Function getRecognisedBulletedStyleNames() As Variant
    Dim strArray(0 To 6) As String

    strArray(0) = "List Bullet"
    strArray(1) = "List Bullet 2"
    strArray(2) = "List Bullet 3"
    strArray(3) = "Table Text Bulleted 1"
    strArray(4) = "Table Text Bulleted 2"
    strArray(5) = "Table Text Bulleted 3"
    strArray(6) = "Pull Out Box Bullet"

   ‘this real list is quite long

    getRecognisedBulletedStyleNames = strArray

End Function

Open in new window

0
Comment
Question by:Fi69
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39703978
Leaving your structure intact for visibility try the following:

Sub BulletReplacement()

    Dim para As Paragraph
    Dim astrWords()  As String
    Dim dict As Object
    Dim strStyle
   
'Return array containing paras not to search
   astrWords = getRecognisedBulletedStyleNames
   Set dict = CreateObject("scripting.dictionary")
   dict.comparemode = vbTextCompare
   For Each itm In astrWords
        If Not dict.Exists(itm) Then
            dict.Add itm, itm
        End If
    Next
    

    For Each para In ActiveDocument.Paragraphs
    'HELP HERE if style name contains the word “bullet” or “Bullet” but is NOT one of the astrWords
        strStyle = LCase(para.Style.NameLocal)
        If InStr(strStyle, "bullet") <> 0 Then
            If Not dict.Exists(strStyle) Then
                para.Style = "List Bullet"
            End If
        End If
    Next para

End Sub


Function getRecognisedBulletedStyleNames() As Variant
    Dim strArray(0 To 6) As String

    strArray(0) = "List Bullet"
    strArray(1) = "List Bullet 2"
    strArray(2) = "List Bullet 3"
    strArray(3) = "Table Text Bulleted 1"
    strArray(4) = "Table Text Bulleted 2"
    strArray(5) = "Table Text Bulleted 3"
    strArray(6) = "Pull Out Box Bullet"

   'this real list is quite long

    getRecognisedBulletedStyleNames = strArray

End Function

Open in new window


Chris
0
 
LVL 15

Accepted Solution

by:
DrTribos earned 2000 total points
ID: 39708166
Another way is to use the filter function on your array...  

This approach looks first for the string bullet, then if it finds that it looks at your array to see if it it one of the built in styles.

For each para....
If InStr(para.Style, "bullet") <> 0 Then
  If ubound(filter (astrWords(), para.Style)) > -1 then  ' Filter returns an array index
  ' do nothing this is a built in bullet
  Else
  ' this is not a builtin style, change to list bullet
    para.Style = "List Bullet"
  End If
End If 

Open in new window

0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39708169
BTW if I'm right and you are making an array of built in styles, you may wish to consider this approach instead:

Dim styleLoop As Style 
 
For Each styleLoop in ActiveDocument.Styles 
 If styleLoop.BuiltIn = False Then 
 Msgbox styleLoop.NameLocal 
 End If 
Next styleLoop

Open in new window


HTH
0
 

Author Comment

by:Fi69
ID: 39708193
Dr Tribos, the filter function works a treat! Thank you.

Regarding your suggestion on the built in styles. Does built in mean "custom styles I've built into the template" or purely Word's built in styles (the default styles essentially)? Because I have a range of custom styles, eg Table Text Bulleted 1, "Table Text Bulleted 2", "Table Text Bulleted 3" and others that I need to scan for.
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39708301
You're welcome. Built in styles are the ones that come with Word. Built in styles are difficult/ impossible to delete but can be modified.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question