RichTextBox check if .seltext contains an image

Hi Experts
How can i find if .seltext contains an image?
I have tried the following:
When viewed in break test= "    " 4 spaces
MsgBox VarType(test)<- This returns a 8 = string  
 x = Len(myrtf1.SelText)<-This always returns 4
Who is Participating?
rettiseertConnect With a Mentor Commented:
A little slow, but I think it works:

if CountImagesSelected(RichTextBox1) > 0 then
   MsgBox "There are images selected"
   MsgBox "No images selected"
end if

Private Function CountImagesSelected(rtb As RichTextBox) As Long

    Dim LastFound As Long
    Dim Count As Long
        LastFound = InStr(LastFound + 1, rtb.SelRTF, "{\pict\")
        If LastFound Then
            If LastFound > 7 Then
                If Mid(rtb.SelRTF, LastFound - 6, 6) = "result" Then
                    'Another object, not picture
                    Count = Count + 1
                End If
                Count = Count + 1
            End If
        End If
    Loop While LastFound <> 0
    CountImagesSelected = Count
End Function
isnoend2001Author Commented:
Hi  rettiseert
You are the man!, just what i was looking for
Thank you

I think another way is to use a hidden RichTextBox and copy there the SelRTF of the original control and then loop through the objects:

    HiddenRichTextBox.TextRTF = OriginalRichTextBox.SelRTF
    Dim O As RichTextLib.OLEObject
    For Each O In HiddenRichTextBox.OLEObjects
        If O.Class = "StaticMetafile" Then
            'This is a picture, increase counter
            CountP = CountP + 1
        End If
isnoend2001Author Commented:
What you already provided worked fine. I only need to check the last line in seltext. Here's why
I made a sectioned rtf file with end of section string and then the name of the section taken from a combo box appended
to the end of the end of section. This provided me with 1 rtf file instead a lot of rtf files. Everything worked fine until i
discovered that if the last string in the section was bulleted it would put a few litttle dots at the end the section. I tried to
overcome this by when saving turning off the bullets like selbullet=False. Can't remember why, but this did not work.
So i decided to check the last row after opening for isnumeric, strings and your image code. This seems to get rid of the little bullets and solve the problem
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.