sorting in the year to the whole document using style

Can we sort references year wise based on style ("pub_year") in the year to the whole document?

Kopp, C.B. (2015). Emotional distress and control in young children.
Aopp, and P.P. (1992). Emotional distress and control in young children.
Lopp, and Williams, P. (2017). Emotional distress and control in young children.
Popp, A.B. (1996). Emotional distress and control in young children.
Andrew SpicerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
Do all references have years in the same format?
Do you want to sort ascending or descending?

A sample Word document would be helpful.
0
ITSysTechSenior Systems AdministratorCommented:
First you need to change the reference style to "ISO 690 - Numerical Reference" then sort by Paragraphs and Number then hit okay and it will sort your information (You can chose Ascending or Descending when the Sort Text box appears).

1.JPG2.JPG
0
Andrew SpicerAuthor Commented:
I don't have sample document now, reference text posted above. Need to sort style (pub_year) based.
0
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Andrew SpicerAuthor Commented:
I have 2000 documents so it's not possible to edit each document. I need  VBA code for macro.
0
Andrew SpicerAuthor Commented:
All references have years in the same format but years might appear multiple in a line so style(pub_year) based ascending order is a right way to sort.
0
Andrew SpicerAuthor Commented:
Sample document attached now. Years have been strong style, not a pub_year style. Need to sort ascending order based on year.
Sample.docx
0
aikimarkCommented:
I don't see a style ("pub_year") applied to any of those bibliography paragraphs.
0
Andrew SpicerAuthor Commented:
Sample document attached. Years have been strong style, not a pub_year style.
0
Andrew SpicerAuthor Commented:
Years have been strong style, not a pub_year style. Sry I mentioned wrongly. Sample document attached now.
Sample.docx
0
aikimarkCommented:
If you first select the bibliography paragraphs, then this routine will sort them by year, replacing the selection.
Sub Q_29068867()
    Dim rng As Range
    Dim rngPP As Paragraph
    Dim lngMinYear As Long
    Dim lngMaxYear As Long
    Dim lngYear As Long
    
    lngMinYear = 9999
    lngMaxYear = -1
    
    Dim colYears() As Collection
    
    Dim oRE As Object
    Dim oMatches As Object
    Dim oM As Object
    Dim oSM As Object
    Dim lngSM As Long

    Set oRE = CreateObject("vbscript.regexp")
    oRE.Global = False
    oRE.Pattern = "\((\d{4})\)"
    
    For Each rngPP In Selection.Paragraphs
        If oRE.test(rngPP.Range.Text) Then
            Set oMatches = oRE.Execute(rngPP.Range.Text)
            Set oM = oMatches(0)
            lngYear = CLng(oM.submatches(0))
            If lngYear < lngMinYear Then
                    lngMinYear = lngYear
            End If
            If lngYear > lngMaxYear Then
                    lngMaxYear = lngYear
            End If
        End If
    Next
    
    ReDim colYears(lngMinYear To lngMaxYear)
    For Each rngPP In Selection.Paragraphs
        If oRE.test(rngPP.Range.Text) Then
            Set oMatches = oRE.Execute(rngPP.Range.Text)
            lngYear = CLng(oMatches(0).submatches(0))
            If colYears(lngYear) Is Nothing Then
                Set colYears(lngYear) = New Collection
            End If
            colYears(lngYear).Add rngPP
        End If
    Next
    
    For lngYear = lngMinYear To lngMaxYear
        If colYears(lngYear) Is Nothing Then
        Else
            For Each rngPP In colYears(lngYear)
                If rng Is Nothing Then
                    Set rng = rngPP.Range
                Else
                    rng = rng & rngPP.Range
                End If
            Next
        End If
    Next
    
    Selection.Range = rng

End Sub

Open in new window

0
ITSysTechSenior Systems AdministratorCommented:
One last follow up question. How are your .docx files named?
0
Andrew SpicerAuthor Commented:
Hi Akimark,

I already said the year might be two or more cases so need to code based on style(Strong), not a parenthesis. Your code is not working if more than one year present in the line.

Example:

William Oasted (2015). Good practices in school readiness. Accepted date (2017) May.
0
aikimarkCommented:
In the document you posted, all the bold/strong date text is in the first value.  If you did not post representative data, please upload a new document that covers the combinations of data you need covered.
0
Rebecca ShabadCommented:
Please add the below line in the end of the sample document & apply 2015 in strong style as per previous references in the sample document & all bold/strong text is a strong style not a normal bold. If you want to check style select the date text & apply Ctrl-Shift-S(style pane). Need to sort ascending year based on style.

William Oasted (2015). Good practicdate school readiness. Accepted date (2017) May.
0
Andrew SpicerAuthor Commented:
Thank you rebecca you did understand my need. Hi Aikimark I think Rebbeca clearly explained my need.
0
aikimarkCommented:
please test this
Sub Q_29068867()
    Dim rng As Range
    Dim rngPP As Paragraph
    Dim rngFind As Range
    Dim boolFound As Boolean
    
    Dim lngMinYear As Long
    Dim lngMaxYear As Long
    Dim lngYear As Long
    
    lngMinYear = 9999
    lngMaxYear = -1
    
    Dim colYears() As Collection
    
    For Each rngPP In Selection.Paragraphs
        Set rngFind = rngPP.Range
        rngFind.Find.ClearFormatting
        rngFind.Find.Style = ActiveDocument.Styles("Strong")
        rngFind.Find.Execute findtext:="^#^#^#^#", Format:=True
        
        If rngFind.Find.Found Then
            lngYear = CLng(rngFind.Text)
            If lngYear < lngMinYear Then
                lngMinYear = lngYear
            End If
            If lngYear > lngMaxYear Then
                lngMaxYear = lngYear
            End If
        End If
    Next
    
    ReDim colYears(lngMinYear To lngMaxYear)
    For Each rngPP In Selection.Paragraphs
        Set rngFind = rngPP.Range
        rngFind.Find.ClearFormatting
        rngFind.Find.Style = ActiveDocument.Styles("Strong")
        rngFind.Find.Execute findtext:="^#^#^#^#", Format:=True
        
        If rngFind.Find.Found Then
            lngYear = CLng(rngFind.Text)
            If colYears(lngYear) Is Nothing Then
                Set colYears(lngYear) = New Collection
            End If
            colYears(lngYear).Add rngPP
        End If
    Next
    
    For lngYear = lngMinYear To lngMaxYear
        If colYears(lngYear) Is Nothing Then
        Else
            For Each rngPP In colYears(lngYear)
                If rng Is Nothing Then
                    Set rng = rngPP.Range
                Else
                    rng = rng & rngPP.Range
                End If
            Next
        End If
    Next
    
    Selection.Range = rng

End Sub

Open in new window

0
Andrew SpicerAuthor Commented:
Thank you aikimark, it's working but strong style not retained after the sorting.
0
aikimarkCommented:
That's interesting
0
Andrew SpicerAuthor Commented:
Hi Aikimark

So it's not possible to retain styles?
0
aikimarkCommented:
It may be possible.  Let me take another crack at it.
0
aikimarkCommented:
Although it isn't as efficient as I would have liked, it does seem to reorder the paragraphs, preserving the formatting.
Sub Q_29068867()
    Dim rng As Range
    Dim rngPP As Paragraph
    Dim rngFind As Range
    Dim boolFound As Boolean
    Dim PPnew As Paragraph
    
    Dim lngMinYear As Long
    Dim lngMaxYear As Long
    Dim lngYear As Long
    
    lngMinYear = 9999
    lngMaxYear = -1
    
    Dim colYears() As Collection
    
    For Each rngPP In Selection.Paragraphs
        Set rngFind = rngPP.Range
        rngFind.Find.ClearFormatting
        rngFind.Find.Style = ActiveDocument.Styles("Strong")
        rngFind.Find.Execute findtext:="^#^#^#^#", Format:=True
        
        If rngFind.Find.Found Then
            lngYear = CLng(rngFind.Text)
            If lngYear < lngMinYear Then
                lngMinYear = lngYear
            End If
            If lngYear > lngMaxYear Then
                lngMaxYear = lngYear
            End If
        End If
    Next
    
    ReDim colYears(lngMinYear To lngMaxYear)
    For Each rngPP In Selection.Paragraphs
        Set rngFind = rngPP.Range
        rngFind.Find.ClearFormatting
        rngFind.Find.Style = ActiveDocument.Styles("Strong")
        rngFind.Find.Execute findtext:="^#^#^#^#", Format:=True
        
        If rngFind.Find.Found Then
            lngYear = CLng(rngFind.Text)
            If colYears(lngYear) Is Nothing Then
                Set colYears(lngYear) = New Collection
            End If
            colYears(lngYear).Add rngPP
        End If
    Next
    
    Application.ScreenUpdating = False
    For lngYear = lngMinYear To lngMaxYear
        If colYears(lngYear) Is Nothing Then
        Else
            For Each rngPP In colYears(lngYear)
                Set PPnew = Selection.Paragraphs.Add
                rngPP.Range.Cut
                PPnew.Range.Paste
            Next
        End If
    Next
    Application.ScreenUpdating = True

End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aikimarkCommented:
solution to problem
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.