Consulting

Results 1 to 5 of 5

Thread: Macro for finding en dash surrounded by text without spaces

  1. #1

    Macro for finding en dash surrounded by text without spaces

    Hello,

    I’m trying to create macro in Word that would find and highlight only following scenario:

    anyText–anyText

    (word, no space, en dash, followed by another word without space).

    in fact, even that would be enough:

    anyLetterEndingString–anyLetterStartingString

    I need to be able to locate en dash when it's surrounded by text without spaces.

    I will be very grateful for any tips, since I'm stuck...

    ----

    Here's the last of my attempts, just for reference:


    Sub Testt()


    Set r = ActiveDocument.Range
    With r.Find
    .ClearFormatting
    .Text = "(< *)(^0150)(* >)"
    .MatchWildcards = True
    Do While .Execute(Forward:=True) = True
    r.HighlightColorIndex = wdPink
    Loop
    End With






    End Sub






    Best regards

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Try this

    I'm pretty sure that the Word VBA gurus that hang out here can improve it, but it seems to work


    Option Explicit
    
    
    Sub Test2()
        
        With Selection
        
            .HomeKey Unit:=wdStory
                    
            .Find.ClearFormatting
            
            .Find.Text = "[! ]^=[! ]"
            .Find.Forward = True
            .Find.Wrap = wdFindContinue
            .Find.MatchWildcards = True
            
            Do While .Find.Execute(Forward:=True)
                .MoveLeft Unit:=wdCharacter, Count:=1
                .MoveRight Unit:=wdCharacter, Count:=1
                .MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
                .Range.HighlightColorIndex = wdPink
                .MoveRight Unit:=wdCharacter, Count:=1
            Loop
            
        End With
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Quote Originally Posted by Paul_Hossler View Post
    Try this

    I'm pretty sure that the Word VBA gurus that hang out here can improve it, but it seems to work


    Option Explicit
    
    
    Sub Test2()
        
        With Selection
        
            .HomeKey Unit:=wdStory
                    
            .Find.ClearFormatting
            
            .Find.Text = "[! ]^=[! ]"
            .Find.Forward = True
            .Find.Wrap = wdFindContinue
            .Find.MatchWildcards = True
            
            Do While .Find.Execute(Forward:=True)
                .MoveLeft Unit:=wdCharacter, Count:=1
                .MoveRight Unit:=wdCharacter, Count:=1
                .MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
                .Range.HighlightColorIndex = wdPink
                .MoveRight Unit:=wdCharacter, Count:=1
            Loop
            
        End With
    End Sub
    Thank you, that works perfectly!

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Paul_Hossler View Post
    I'm pretty sure that the Word VBA gurus that hang out here can improve it, but it seems to work
    Indeed:
    Sub DSFinder()
    Application.ScreenUpdating = False
    Dim iDx As Long
    iDx = Options.DefaultHighlightColorIndex
    Options.DefaultHighlightColorIndex = wdPink
    With ActiveDocument.Range.Find
      .MatchWildcards = True
      .Text = "<[! ]@^=*>"
      .Replacement.Text = "^&"
      .Replacement.Highlight = True
      .Execute Replace:=wdReplaceAll
    End With
    Options.DefaultHighlightColorIndex = iDx
    Application.ScreenUpdating = True
    End Sub
    Cross-posted (and answered) at: https://stackoverflow.com/questions/...42737#60242737
    @rook: Kindly read VBA Express' policy on Cross-Posting in Rule 3: http://www.vbaexpress.com/forum/faq...._new_faq_item3
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Thank you, will keep in mind for the future.

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
  •