Page 1 of 1

Document2\execCommand with Copy

Posted: Mon Sep 12, 2005 10:18 pm
by DevilDog
Has anyone used the Document2\execCommand command to copy the currently selected text in Internet Explorer?

I've tried

Code: Select all

iCopy.l = ANSI2BSTR("Copy")
  If Document2\execCommand(@iCopy,#False,0,0) = #S_OK
with no luck. I have a reference to the current open instance of Internet Explorer IHTMLdocument2 but can't figure out a way to copy the currently selected text.

I've also looked into using the IHTMLSelection object but couldn't figure out how the TextRange (created with the createRange command) object can return the selected text.

Can anyone help please? :?

Posted: Mon Sep 12, 2005 11:19 pm
by FloHimself
try:

Code: Select all

iCopy.l = ANSI2BSTR("Copy") 
  If Document2\execCommand(iCopy,#False,0,0) = #S_OK

Posted: Mon Sep 12, 2005 11:30 pm
by DevilDog
No dice,
I tried also changing the #False to 0 and the Ansi2Bstr to Ansi2Uni with no luck. UGH!

Are we breaking new ground here? Exploring uncharted territory? Has anyone done this before?

I wonder because I saw Freak's IETool source code where he keyboards a Ctrl+C to copy the selected text into the clipboard. Is this why he did it that way?

I mean I guess it's possible to do it that way but I think I would need a reference to the IWebbrowser2 interface which I currently do not have. I'm one level down in the IHTMLDocument2 level.

Any other ideas? Anyone?

Posted: Mon Sep 12, 2005 11:32 pm
by FloHimself
can you post whole source or example?

Posted: Mon Sep 12, 2005 11:37 pm
by DevilDog
OK, first I get a handle to the "Internet Explorer_Server" window in the current running instance of IE and I pass it to the code below (provided by Freak)

Code: Select all

Procedure.l GetIHTMLDocument2(ExplorerServerWindow) 

  HtmlDoc.IHTMLDocument2 = 0 
      
  OleAcc = OpenLibrary(#PB_Any, "OLEACC.DLL") 
  If OleAcc And IsFunction(OleAcc, "ObjectFromLresult") 

    Message = RegisterWindowMessage_("WM_HTML_GETOBJECT") 
    SendMessageTimeout_(ExplorerServerWindow, Message, 0, 0, #SMTO_ABORTIFHUNG, 1000, @MessageResult) 
      
    CallFunction(OleAcc, "ObjectFromLresult", MessageResult, ?IID_IHTMLDocument2, 0, @HtmlDoc)    
 
    CloseLibrary(OleAcc) 
  EndIf 
  
  ProcedureReturn HtmlDoc 
EndProcedure 
This works great and I do end up with a pointer to the IHTMLDocument2 interface.

From there I'm just simply trying to run the code:

Code: Select all

iCopy.l = ANSI2BSTR("Copy") 
  If Document2\execCommand(iCopy,#False,0,0) = #S_OK

Posted: Tue Sep 13, 2005 12:11 am
by FloHimself
you should call it like:

Code: Select all

Command.VARIANT
Command\vt = #VT_BSTR
Command\bstrVal = Ansi2Uni("copy")
      
If Document2\execCommand(@Command, #VARIANT_FALSE, 0, @Success) = #S_OK 
  Debug Success
EndIf
see: http://msdn.microsoft.com/library/defau ... ommand.asp

but:

Code: Select all

If Document2\queryCommandEnabled(Ansi2Uni("Copy"), @Success) = #S_OK
  Debug Success
EndIf
returns #VT_FALSE, so it seem you can't call this command.
see: http://msdn.microsoft.com/library/defau ... nabled.asp

but maybe i am wrong, i am not into this COM stuff.

Posted: Tue Sep 13, 2005 1:19 am
by freak
You can probably only call this on the execCommand() method of the selected TextRange object itself.

> I wonder because I saw Freak's IETool source code where he keyboards a Ctrl+C to copy the selected text into the clipboard. Is this why he did it that way?

The reason is that i wrote that tool a long time ago and was quite new to windows programming in general.
It was just the simplest solution.

Not many people have done this in PB so far (as far as i know). I do not
have a real use for all this IE stuff, so i only experiment with it when others have a question.

I have put an example here on reading the selected text without the clipboard:
viewtopic.php?p=103173#103173