Consulting

Results 1 to 4 of 4

Thread: Find and replace if specific conditions aply

  1. #1
    VBAX Newbie
    Joined
    May 2023
    Posts
    2
    Location

    Question Find and replace if specific conditions aply

    I need help for checking citations. I have to check long documents if "cf." Is used correctly. I either need a way to highlight the brackets or sentences in which "cf." Is used or for the VBA to do it itself.
    If there is a "." Or any word in front of the bracket it needs "cf.". If there are quotation marks in front of the bracket there should be no "cf."
    For example this is correct:
    example. (cf. Weber 2010)
    Example (cf. Weber 2010)
    This is incorrect:
    Example" (cf. Weber 2010)
    Example. (Weber 2010)


    I tried something to highlight sentences with quotation marks, but it is not doing what I want, please help

    Sub CheckParentheses()
    ' CheckParentheses Makro
    Dim paragraph As paragraph
        Dim Doc As Document
    ' Set the document object to the active document
        Set Doc = ActiveDocument
    ' Loop through all paragraphs in the document
        For Each paragraph In Doc.Paragraphs
            ' Check if the paragraph has the "Standard" style and does not contain a parenthesis
            If paragraph.Style = "Standard" And InStr(paragraph.Range.Text, "(") = 0 And InStr(paragraph.Range.Text, ")") = 0 Then
                ' Highlight the paragraph in yellow
                paragraph.Range.HighlightColorIndex = wdYellow
            End If
        Next
    End Sub
    Last edited by Aussiebear; 05-08-2023 at 12:09 PM. Reason: Added code tags to supplied code

  2. #2
    Why not just fix the errant references e.g.

    Sub Macro1()
    Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(findText:="? \(", MatchWildcards:=True)
                With oRng
                    .End = .End + 4
                    If .Characters(1).Text = Chr(34) Then
                        .Text = Replace(.Text, "cf. ", "")
                        .Paragraphs(1).Range.HighlightColorIndex = wdYellow
                    Else
                        If InStr(1, .Text, "cf. ") = 0 Then
                            .End = .End - 4
                            .InsertAfter "cf. "
                            .Paragraphs(1).Range.HighlightColorIndex = wdYellow
                        End If
                    End If
                    .Collapse 0
                End With
            Loop
        End With
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    May 2023
    Posts
    2
    Location
    Hi,
    Thank you for your reply. The macro does something, but it changes all instances. If there are quotation marks before the bracket I don't want the macro to add cf.

    Quote Originally Posted by gmayor View Post
    Why not just fix the errant references e.g.

    Sub Macro1()
    Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(findText:="? \(", MatchWildcards:=True)
                With oRng
                    .End = .End + 4
                    If .Characters(1).Text = Chr(34) Then
                        .Text = Replace(.Text, "cf. ", "")
                        .Paragraphs(1).Range.HighlightColorIndex = wdYellow
                    Else
                        If InStr(1, .Text, "cf. ") = 0 Then
                            .End = .End - 4
                            .InsertAfter "cf. "
                            .Paragraphs(1).Range.HighlightColorIndex = wdYellow
                        End If
                    End If
                    .Collapse 0
                End With
            Loop
        End With
    End Sub

  4. #4
    If your examples accurately reflect what is in the document then cf is not added, but removed if present. Post a sample document..
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Tags for this Thread

Posting Permissions

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