Bug + Clipboard Problems


I have, in my current project a Form with a RichTextBox control placed on it. A user is able to open a new document and type into it, or open a text file and have the text placed into another instance of this RichTextBox control.

Once this happens, the user may select a menu or click on a button which invokes a function named CheckSpelling() which is coded in the General declaration of the Form which contains this control.

Here are the problems:

In the frmMDIMain's Option Explicit section, the following is declared:

Private actCtl As Control

The spell checker part of the program works. It starts Word and takes the text within the control and copies it into Word and then invokes the spell checker of Word(97).

When it exits, the program crashes. It crashes two different ways.

1st, within the mnuEditPaste_Click event it crashes with this -
 "object doesn't support property or method"

OR 2nd, it crashes with this -

 "Can't open Clipboard"
    actCtl.SelText Clipboard.GetText

I understand the first error, because what is being attempted here is to determine what the active control here is and SelText is only a method of a TextBox, ComboBox, ImageBox or a RichTextBox.

I changed this variable declaration to -

Private actCtl As RichTextBox

This worked. For the very first time, I could type something into a new document, invoke the CheckSpelling() function, make corrections and have those corrections pasted back into the original document.

Then I tried it again; and again.

Then it crashed with other, less occuring run-time error -

"Can't open Clipboard."

Any thoughs on this actCtl variable and is there some sort of code management needed to be applied here for the Clipboard? My version of MSDN help showed that there were some VB constants for determining what type of data was to be sent (copied) to the Clipboard, vbCFText, but I couldn't find any more info on it. The Object Browser didn't include any info on these constants.

With actCtl As RichTextBox,

what is the problem with...
actCtl.SelText Clipboard.GetText

Who is Participating?
The advantage with not using the MW Word spell checker is that your app wil work on systems without Microsoft Office.
I vote for using the API:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_PASTE = &H302
Private Const WM_COPY = &H301
Private Const WM_CUT = &H300


Private Sub cmdCopy_Click()
  SendMessage txtTextBoxName.hWnd, WM_COPY, 0, 0
End Sub

Private Sub cmdCut_Click()
  SendMessage txtTextBoxName.hWnd, WM_CUT, 0, 0
End Sub

Private Sub cmdPaste_Click()
  SendMessage txtTextBoxName.hWnd, WM_PASTE, 0, 0
End Sub

try this if you dont want to use the API:

actCtl.SelText = Clipboard.GetData
actCtl.SelText = Clipboard.GetText
I would try loading the text into the RTB, then selecting it

with actCtl
  .Text = Clipboard.GetText
  .selstart = 0
  .sellength = len(actCrl.text)
end with
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Why? > Private actCtl As Control

I think Private means only you can access it. What happens if you change as follows?

Public actCtl As Control
Richie_SimonettiIT OperationsCommented:
How about doing this:

actCtl.SelText = Clipboard.GetText, vbRichtext 'Can't remember which one.
'Just check out the clipboard formats and choose the one that's richtext format.

just a note: Make sure that MS Word, excel and powerpoint is NOT running that that time as those programs would like to dominate the clipboard (don't know why there's such a need) and it would deny all accesses from many other VB programs.

On the other hand, you may want to try this:

Dim MyString as String
MyString = Clipboard.GetText
actctl.SelText = My String

Yet on the other hand, if you're quite sure the user doesn't have text (check using the instr method) thaqt's { } or + then use sendkeys to send the stuff:

actctl.SetFocus (if there's such a property)
sendkeys Clipboard.gettext

That's it!

glass cookie : )
jnowlinAuthor Commented:
Well JonFish85,

Your API code doesn't crash the program. I substituted:

SendMessage frmRTFDisplay.rtfDisplay.hWnd, WM_PASTE, 0, 0

for the original call:
SendMessage txtTextBoxName.hWnd, WM_PASTE, 0, 0

I fire up a new frmRTFDisplay and type some characters into it and click on the spell checker. Word comes up, whether I select Cancel or Change, the only thing that happens is that I get a new instance of the Form. I close this. I close the original Form, the one that got the text entered into it, and my QueryUnload event fires and prompts me that there were changes made, although they weren't seen visually.


I tried yours. I still get the run-time error "can't open clipboard." This Clipboard thing is a real brain picker.


Changing the variable to Public from Private made no difference. Origianlly, the idea was to detect the active form. Dim actCtl As Control,
the Control datatype has no SelText method. So long as the app is only using RichTextBox text, actCtl *must* be defined as a RichTextBox....I think
there's such a thing as:
If Screen.ActiveControl = RichTextBox Then......


I tried. This remains a nagging Clipboard problem.

Why cannot the Clipboard be opened sometimes and opened other times? This is a puzzler.
I tried:
Dim bData As Boolean
bData = Clipboard.SetData(vbCFText)
bData = frmRTFDisplay.SetData(vbCFText)

If bData = ? Then ....

The clipboard can be locked and unlocked I suspect that there is a bug in an app which is not handling the clipboard in the best way.

Before you put data on the clipboard you must use the clear method:

Clipboard.Settext "My Text"


The clipboard is a Microsoft Cock-up.  Nobody was realy made responsible for ths clipboard at Microsoft, (or if they were they must have been on night shift).  The Windows 95 and Outlook and VB programmers must have been living on diffenent planets.

The main problem is that Microsoft refuse to publish their internal documentation for it. And so even their own programmers don't know whats going on.

I raised an indecent with Microsoft last year chich lasted for 3 months

All I wanted to do was to drag (copy) and drop a file onto the clipboard or into an Outlook message.

In windows 95 the code they advised me to use deleted the file? And Outook just would not enable the File list (format 15).

After 3 months they begged me to clos the issue and ask my cleinst to upgrade to NT and outlook 2000.


To fix your problem try calling CloseClipboard before you use it.

You will need:

Private Declare Function CloseClipboard Lib "user32" () As Long


How about doing this:

using the vbCFRTF format rather than vbCFText as cvCFText is plain text like in notepad.

On the other hand, place the statement:

On Error Resume Next

Before your Clipboard.Gettext statement.

See if you are still able to get the clipboard contents or not.

The last resort would be to so this:

richtextbox.Setfocus '(if there's such a property)
Sendkeys "^V"

The above code would paste manually rather than programmatically retrieve the clipboard contents.

That's it!

glass cookie : )
Richie_SimonettiIT OperationsCommented:
did you try with:
actCtl.SelRTF Clipboard.GetData

jnowlinAuthor Commented:
Yes Richie_Simonetti, I did try your suggestion.
Please, read on.....

But today, after going 12 rounds with this, when I put in
On Error Resume Next the program didn't crash. Just for kicks, I clicked on my Paste menu item. Guess what..
it pasted the corrected, Word spell-checked text into this RichTextBox control of mine!!

Here's the function CheckSpelling, defined on my frmRTFDisplay:

Public Sub Check_Spelling()
'must have added Word as a reference!

    Dim wordApp As Word.Application
    Dim wordDoc As Word.Document
    Screen.MousePointer = vbHourglass
    Set wordApp = New Word.Application
    ' this allows us to see it work
    wordApp.Visible = True
    Set wordDoc = wordApp.Documents.Add
     On Error Resume Next
    'clear the clipboard
    'close the clipboard using function from 'inthedark'
   'copy text to the clipboard
    Clipboard.SetText rtfDisplay.TextRTF, vbCFRTF
    'paste to word
    'check the spelling
    ' make sure that Word is in the foreground
    'select all of the text that was copied
    'send CTRL+C to copy to the clipboard
    SendKeys "^c", True
    'close Word w/o save
    wordDoc.Close False
    'quit MS Word
    ' put the focus on the RichTextBox control
    'delete original text in the control
    rtfDisplay.Text = ""
    ' paste text from clipboard by calling the mnuEditPaste_Click event
    SendKeys "^v", True
    ' and clear the clipboard
    'destroy the objects
    Set wordDoc = Nothing
    Set wordApp = Nothing

    Screen.MousePointer = vbDefault
End Sub

Now, I added a MsgBox to the mnuEditPaste_Click event as follows:

On Error Resume Next
    MsgBox "Click 'OK' and text will be 'pasted'.", vbOKOnly, "Microsoft Clipboard Problem"
    actCtl.SelRTF = Clipboard.GetText
    actCtl.SelStart = 0

And, you may ask? When I click on the OK button, the corrected text is there!

Was the code running too fast for Microsoft's Windows 2000's Clipboard that I needed to add a message box to slow it down in order for it to work?!?!?

I purchased the Wintertree spell checker, this works really well and didn't cost too much, (I got a customer to pay for it).

jnowlinAuthor Commented:
Is that the WSpell ActiveX Control that you refer to inthedark? It sounds like *THE* solution to this problem.
jnowlinAuthor Commented:
Thanks inthedark.

I opted for the eval version of the WSpell ActiveX control to give it a try.
I had completely ignored the fact that some of us don't have and probably don't want Office95, 97, 2000, XP, etc.

So, I eagerly await the download instructions from the folks at WinterTree. In the meantime, the spell checker works, although it's horrendously choppy and sloppy.
This has been Microsoft's finest hour. Scary. Isn't it?

Jim Nowlin
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.