Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Selecting all instances of a style (Word 2003 onwards)

    Hopefully a very quick question - how do you say the above in VBA? I tried recording the clicks (with styles window showing, down arrow and 'Select all # instances of this style') but the macro was blank!

    Ta ever so!
    Beryl M


  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Selecting all instances of a style (Word 2003 onwards)

    As far as I know, there is no support for this in the Word object model in Word 2003. I don't know about Word 2007.

    You can use the Find object to loop through all instances of a style, or to replace all instances of a style with another style etc.

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Selecting all instances of a style (Word 2003 onwards)

    Sigh. Yup, looks like I'll have to!
    Beryl M


  4. #4
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Selecting all instances of a style (Word 2003 onwards)

    Sigh again. Looks like I'm not as good with loops yet as I hoped I was! Could someone give me some clues as to how to set this up?

    Many thanks!!
    Beryl M


  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Selecting all instances of a style (Word 2003 onwards)

    What do you want to do?

  6. #6
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Selecting all instances of a style (Word 2003 onwards)

    Sorry! Loop through all instances of text with a particular style applied, and basically re-apply it again, although I might have to get a bit more specific (ie remove para numbering) if it ignores the re-applying the way it did the original application! (see the post you moved to Word!)
    Beryl M


  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Selecting all instances of a style (Word 2003 onwards)

    Would this do what you want?

    Sub SearchStyle()
    Dim strStyle As String
    strStyle = "MyStyle"
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .Text = ""
    .ClearFormatting
    .Style = strStyle
    Do While .Execute
    Selection.ParagraphFormat.Reset
    Selection.Font.Reset
    Loop
    End With
    End Sub

  8. #8
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Selecting all instances of a style (Word 2003 onwards)

    LOL - do you know, Hans, I don't know! Your code is so neat and minimalist I sometimes have trouble actually seeing what it does, there's so little of it - you should see what my version looked like ...!

    I presume 'MyStyle' is the name of the style to look for - and then it 'resets' the paragraph format and font to the default for that style, yes?

    If that's the case, does resetting the paragraph formatting force it to accept the presence or absence of numbering?

    And if the answers to both those questions are yes, then by George, I think it does it!!

    I'll have a play and let you know - thank you so much!!
    Beryl M


  9. #9
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Selecting all instances of a style (Word 2003 onwards)

    Just thought - if there were, say, half a dozen styles that you wanted to run this code on, could we dim a manually listed array and run this sub on each one? That would be the most efficient way to run it on half a dozen styles, wouldn't it?
    Beryl M


  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Selecting all instances of a style (Word 2003 onwards)

    MyStyle is the name of the style to look for - replace it with the appropriate name.
    The code resets the paragraph format and character format to the style definition. So if the style definition includes numbering, non-numbered paragraphs will be numbered. But if the style definition does not include numbering, numbered paragraphs will lose their numbering. (For the specified style only, of course)

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Selecting all instances of a style (Word 2003 onwards)

    You could use something like the following, I've included two variations:

    Sub SearchStyle(strStyle As String)
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .Text = ""
    .ClearFormatting
    .Style = strStyle
    .Replacement.Text = ""
    .Replacement.ClearFormatting
    Do While .Execute
    Selection.ParagraphFormat.Reset
    Selection.Font.Reset
    Loop
    End With
    End Sub

    Sub SearchLotsOfStyles1()
    SearchStyle "Style1"
    SearchStyle "OtherStyle"
    SearchStyle "NoStyle"
    End Sub

    Sub SearchLotsOfStyles2()
    Dim arrStyles(1 To 3) As String
    Dim i As Integer
    arrStyles(1) = "Style1"
    arrStyles(2) = "OtherStyle"
    arrStyles(3) = "NoStyle"
    For i = 1 To 3
    SearchStyle arrStyles(i)
    Next i
    End Sub

    You can run either SearchLotsOfStyles1 or SearchLotsOfStyles2.

  12. #12
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Selecting all instances of a style (Word 2003 onwards)

    Poifick!! <img src=/S/groovin.gif border=0 alt=groovin width=21 height=21>
    Beryl M


  13. #13
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Selecting all instances of a style (Word 2003 onwards)

    Hans, you are really something else! How do you carry all this stuff in your head?!

    I'd worked out Lots1, but thought Lots2 would be shorter, because I thought it could be written as arrStyles("firststyle", "secondstyle", "thirdstyle"), rather than doing a line for each one.

    However, it's a wonderful post to keep handy as a basic layout for looping, with and without arrays!

    Many thanks again!
    Beryl M


  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Selecting all instances of a style (Word 2003 onwards)

    You could do this, if you prefer (note that strStyle is now a ByVal argument, otherwise you get an error message):

    Sub SearchStyle(ByVal strStyle As String)
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .Text = ""
    .ClearFormatting
    .Style = strStyle
    .Replacement.Text = ""
    .Replacement.ClearFormatting
    Do While .Execute
    Selection.ParagraphFormat.Reset
    Selection.Font.Reset
    Loop
    End With
    End Sub

    Sub SearchLotsOfStyles3()
    Dim itm
    For Each itm In Array("Style1", "OtherStyle", "NoStyle")
    SearchStyle itm
    Next itm
    End Sub

  15. #15
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Selecting all instances of a style (Word 2003 onwards)

    Hi folks, I might be missing something here, but there doesn't seem to be anything in the loop to move it on one each time (a 'next ...'?) or at least, if there's not supposed to be a next, the code seems to just find the same instance of the style over and over again ...?
    Beryl M


Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •