Word VBA - Find. To find Style 1 or Style 2 or Style 3

hindersaliva
hindersaliva used Ask the Experts™
on
Word VBA - I'm using this code to Find text of Heading 1, 2 and 3 styles.

In a Do .... Loop

        With rngContent.Find
         .ClearFormatting
         .Style = "Heading 1"
         .Execute Forward:=True
        End With

Open in new window


As you can see it will only find ONE style. Is it possible to modify the Find to find "Heading 1" OR "Heading 2" OR "Heading 3"?

I know I can run it 3 times.  But I want the Headings to be found so that when I put them into an Array they are in the same order as they occur on the document. FYI the styles are an Outline in 3 levels.

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Hi,

You could fill a 2D array with what you've found and the position of its first character then sort it by the position

for an example of 2d sorting refer to
https://www.experts-exchange.com/questions/27345467/VBA-Sorting-array-based-on-priority-list.html

Regards

Author

Commented:
Rgonzo, yes a sort would solve it - after looping 3 times. Would that be quick? The array is 5 x 1000 approx. of short string data.
Top Expert 2016
Commented:
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Top Expert 2016
Commented:
you could possibly use ADO to sort even more quickly
an example
Sub Macro()
Dim rstADO As Object
Dim fld As Object

Set rstADO = CreateObject("ADODB.Recordset")
With rstADO
    .Fields.Append "Position", adDouble, , adFldMayBeNull
    .Fields.Append "StyleText", adVarChar, -1, adFldMayBeNull

    .CursorType = adOpenKeyset
    .CursorLocation = adUseClient
    .LockType = adLockPessimistic
    .Open
End With

For Idx1 = 1 To 3
    For Idx2 = 1 To 5000
        rstADO.AddNew
        rstADO.Fields(0) = CDbl(Idx2 & "." & Idx1)
        rstADO.Fields(1) = Idx1 & "-" & Idx2
        rstADO.Update
    Next
Next
rstADO.MoveFirst
rstADO.Sort = "Position ASC"
arrRes = rstADO.getRows(Fields:="StyleText")
Set rstADO = Nothing
End Sub

Open in new window

Author

Commented:
Wow Rgonzo! I never knew of rst.Sort
I didn't know ADO is that powerful.

That means, in cases like this, it is better to use an ADO recordset instead of an array. Am I right?
Or, to sort an array, transfer the data into a recordset, sort, and transfer back into the array?


I have a further idea. I shall post that as a separate question. TBA.
Top Expert 2016
Commented:
I didn't test really  but in an interpreted language like VBA, doing a function oneself is (most of the time) slower (especially if the data needs a lot of work)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial