• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 547
  • Last Modified:

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
  • 2
  • 2
1 Solution
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now