RichTextBox check if .seltext contains an image

Posted on 2005-04-27
Last Modified: 2012-08-14
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
Question by:isnoend2001
    LVL 13

    Accepted 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

    Author Comment

    Hi  rettiseert
    You are the man!, just what i was looking for
    LVL 13

    Expert Comment

    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

    Author Comment

    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

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now