• Using instr command with “[a-zA-Z]”

    Author
    Topic
    #504142

    Hi!
    There ara several frames in my doc. Some of them are pictures and some of them are contain text.
    I want to find if it contain text. How should I use the “instr” command?
    Thanks in advance.

    Code:
    Sub LoopFrames()
    
     Dim frm As Frame
    
     For Each frm In ActiveDocument.frames
     frm.Select
     hah = frm.Range.Text
    
    ********* THIS LINE İS MY PROBLEM****
    if instr(hah, “[a-zA-Z]”) >0 Then
    ‘or I want to do this line
     If hah.Text Like “[a-zA-Z]” = True Then 
    *********************************
     MsgBox hah
     End If
     Next frm
     End Sub
    
    Viewing 4 reply threads
    Author
    Replies
    • #1548358

      Ken,

      VBA’s instr function does not support regular expressions.

      Maybe someone else knows how to search frames but I’ve drawn a blank.

      :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1548363

      Ken,

      Ok, I think I’ve got it now:

      Code:
      Option Explicit
      
      Sub LoopFrames()
      
         Dim frm As Frame
      
         For Each frm In ActiveDocument.Frames
           
            If (frm.Range.Find.Execute(findtext:="[a-z,A-Z]", _
                    Forward:=True, MatchWildcards:=True)) Then
              MsgBox frm.Range
            End If
      
         Next frm
      
      End Sub  'LoopFrames
      

      Here’s my test document:
      43356-kentestdoc

      Running the program produced the two desired msg boxes.

      Here’s the Document w/macro:

      HTH :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1548366

      A more robust way that takes into account anything that might be counted as text:

      Code:
      Sub FrameTextTest()
      Dim Frm As Frame
      For Each Frm In ActiveDocument.Frames
        MsgBox RangeHasText(Frm.Range)
      Next
      End Sub
      
      Function RangeHasText(Rng As Range) As Boolean
      RangeHasText = (Len(Rng.Text) > Rng.InlineShapes.Count)
      End Function

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1548367

      Paul,

      If I read your code correctly In-Line Shapes in the frame count as 1 character each?

      Neat solution, I’d never have gotten there! :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      • #1548368

        If I read your code correctly In-Line Shapes in the frame count as 1 character each?

        Correct. In text terms, they actually express themselves as a /.

        Cheers,
        Paul Edstein
        [Fmr MS MVP - Word]

    • #1548377

      Thanks to both of you.

    Viewing 4 reply threads
    Reply To: Reply #1548368 in Using instr command with “[a-zA-Z]”

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel