Solved

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

Posted on 2013-12-07
5
248 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

831 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