Consulting

Results 1 to 2 of 2

Thread: VBA Newbie Trying to Find/Replace All Caps after Tab

  1. #1

    VBA Newbie Trying to Find/Replace All Caps after Tab

    Hi, everyone.

    I'm trying to create a macro in Word that will quickly find/replace instances in documents where certain characters followed by a tab are always followed by a capital letter.

    For instance:

    Q (tab) hello
    A (tab) goodbye

    Would always be changed to

    Q (tab) Hello
    A (tab) Goodbye

    I tried (silly me) using the Find/Replace tab designator of ^t in a trial macro, but then realized I probably needed to use vbtab in some fashion, but can't seem to get the syntax right.

    So far I've gotten

    With ActiveDocument.Range.Find
            .ClearFormatting
            With .Replacement.Font
                .SmallCaps = False
                .AllCaps = True
            End With
    
    
            .MatchWildcards = True
            .Text = "Q" &vbtab "([a-z])"
            .Replacement.Text = "Q" &vbtab "\1"
            .Execute Replace:=wdReplaceAll
        End With
    But that is not working at all, haha. I'm not sure how to only select Q and A as the character preceding a tab, and how to continue the syntax to include any lowercase character after the tab.
    Last edited by Aussiebear; 10-10-2022 at 03:39 PM. Reason: Added code tags to supplied code

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You could use a single wildcard Find/Replace, where:
    Find = [QA]^t[a-z]
    Replace = ^&
    and you set the replacement font characteristic to All Caps.

    There is no need for a macro. That said, as a macro:
    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "[QA]^t[a-z]"
      .Replacement.Text = "^&"
      .Replacement.Font.SmallCaps = False
      .Replacement.Font.AllCaps = True
      .Forward = True
      .Wrap = wdFindContinue
      .Format = True
      .MatchWildcards = True
      .Execute Replace:=wdReplaceAll
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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