View Single Post
 
Old 02-11-2023, 04:01 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The second list contains more items than the first, but as far as the first list goes, select the list then run the following macro:


Code:
Sub FixSelectedReferences()
Dim vList As Variant, vItem As Variant
Dim i As Integer, j As Integer
Dim oDoc As Document, oTmp As Document
    vList = Split(Selection.Text, ";")
    Set oDoc = ActiveDocument
    Set oTmp = Documents.Add
    For i = 0 To UBound(vList)
        vItem = Split(vList(i), ",")
        For j = 0 To UBound(vItem)
            oTmp.Range.InsertAfter Trim(vItem(j))
            If j < UBound(vItem) Then oTmp.Range.InsertParagraphAfter
        Next j
        If i < UBound(vList) Then oTmp.Range.InsertParagraphAfter
    Next i
    Selection.Text = FixList(oTmp)
lbl_Exit:
    Set oDoc = Nothing
    Set oTmp = Nothing
    Exit Sub
End Sub

Private Function FixList(oTmp As Document) As String
Dim oPara As Paragraph
Dim oRng As Range
Dim i As Integer
Dim sRef As String
    For i = 1 To oTmp.Range.Paragraphs.Count
        Set oPara = oTmp.Range.Paragraphs(i)
        Set oRng = oPara.Range
        oRng.Collapse (1)
        If IsNumeric(Left(oPara.Range, 1)) = False Then
            oRng.MoveEndUntil Chr(32)
            sRef = oRng.Text & Chr(32)
        Else
            oRng.Text = sRef
        End If
    Next i
    Set oRng = oTmp.Range
    With oRng.Find
        .Text = "^p"
        .Replacement.Text = "; "
        .Execute Replace:=wdReplaceAll
    End With
    Set oRng = oTmp.Paragraphs(1).Range
    oRng.End = oRng.End - 3
    FixList = oRng.Text
    oTmp.Close 0
lbl_Exit:
    Set oRng = Nothing
    Set oPara = Nothing
    Exit Function
End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote