Solved

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

Posted on 2013-12-07
5
245 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
Comment Utility
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 14

Accepted Solution

by:
DrTribos earned 500 total points
Comment Utility
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 14

Expert Comment

by:DrTribos
Comment Utility
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
Comment Utility
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 14

Expert Comment

by:DrTribos
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now