Results 1 to 7 of 7
  1. #1
    Lounger
    Join Date
    Jul 2008
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Using instr command with "[a-zA-Z]"

    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

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Ken,

    VBA's instr function does not support regular expressions.

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

    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    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:
    kentestdoc.PNG

    Running the program produced the two desired msg boxes.

    Here's the Document w/macro: KenTestDoc.docm

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  4. The Following User Says Thank You to RetiredGeek For This Useful Post:

    yken (2016-01-22)

  5. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    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
    [MS MVP - Word]

  6. The Following User Says Thank You to macropod For This Useful Post:

    RetiredGeek (2016-01-21)

  7. #5
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    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!
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  8. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by RetiredGeek View Post
    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
    [MS MVP - Word]

  9. #7
    Lounger
    Join Date
    Jul 2008
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks to both of you.

Posting Permissions

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