Home > Enterprise >  Extract in-text citation inside word documents
Extract in-text citation inside word documents

Time:10-26

my purpose is to find author and the year (four digit) inside parenthesis in a word document through regex or wildcards and format the color (change from black to red) to select them through select similar text my sample to change italic and only all text inside parenthesis:

Dim Rng As Range
Dim n As Long
Application.ScreenUpdating = False
n = Selection.End
With Selection.Find
    .MatchWildcards = True
    .ClearFormatting
    .Wrap = wdFindStop
    .text = "\(*\)"
    Do While .Execute
        Set Rng = Selection.Range
        If Rng.Start > n Then Exit Do
        Rng.MoveStart unit:=wdCharacter, count:=1
        Rng.MoveEnd unit:=wdCharacter, count:=-1
        Rng.Font.Italic = True
    Loop
End With
Application.ScreenUpdating = True
End Sub

CodePudding user response:

The following code may point you in the right direction

Option Explicit

Sub Test()

    SetAuthorTextColour WdColorIndex.wdRed
End Sub

Sub SetAuthorTextColour(ByVal ipColour As WdColorIndex)

    Dim myText As Word.Range
    With ActiveDocument.StoryRanges(wdMainTextStory)
    
        Do
            With .Find
            
                .MatchWildcards = True
                .Text = "([(])(*)([0123456789]{4,4})(*)([)])"
                .ClearFormatting
                .Format = True
                
                .Wrap = wdFindStop
                .Execute
                
            End With
            
            If .Find.Found Then
            
                Set myText = .Duplicate
                myText.MoveStart unit:=wdCharacter, Count:=1
                myText.MoveEnd unit:=wdCharacter, Count:=-1
                ' you may wish to change to using colorindex as
                ' I think color is deprecated.
                myText.Font.ColorIndex = ipColour
                
            End If
            
        Loop While .Find.Found
    
    End With
    
End Sub
  • Related