Solved

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

Posted on 2013-12-07
5
247 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
  • 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 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
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.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

805 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