Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

730 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