Page 1 of 1

Copy file to the Clipboard

Posted: Sun Oct 21, 2007 2:05 pm
by Marco2007
Hi,

is there any chance to copy a file to the clipboard, then runprogram("word.doc") and just press ctrl-v to paste the file?

just like that:

Image

I originally posted that on the German forum: http://www.purebasic.fr/german/viewtopic.php?t=14603

thanx
Marco

Posted: Tue Oct 30, 2007 2:02 am
by Sparkie
I've pieced this together from various sources (Google). It's a long shot but it might be worth a try. I don't have Word here so I can't say if it works or not with the #CF_HDROP format. It does NOT work with WordPad nor OpenOffice. :(

(It DOES work for pasting a file into Explorer :) )

Code: Select all

Procedure CopyFile2ClipBoard(file.s)
  clipFile = 0
  If OpenClipboard_(0)
    EmptyClipboard_()
    hGlobal = GlobalAlloc_(#GHND, SizeOf(DROPFILES) + Len(file)  + 2)
    If hGlobal
      *lpGlobal.DROPFILES = GlobalLock_(hGlobal)
      ZeroMemory_(*lpGlobal, SizeOf(DROPFILES))
      *lpGlobal\pFiles = SizeOf(DROPFILES)
      *lpGlobal\fWide = 0
      *lpGlobal\fNC = 0
      *lpGlobal\pt\x = 0
      *lpGlobal\pt\y = 0
      CopyMemory_((*lpGlobal + SizeOf(DROPFILES)), @file, Len(file) + 2)
      GlobalUnlock_(hGlobal)
      If SetClipboardData_(#CF_HDROP, hGlobal)
        ;SetClipboardData_(#CF_TEXT, file)
        clipFile = #True
      EndIf
    EndIf
    CloseClipboard_()
  EndIf
  ProcedureReturn clipFile
EndProcedure

file2copy.s = OpenFileRequester("Select file to copy to the ClipBoard", "", "All files|*.*", 0)  
If file2copy
  CopyFile2ClipBoard(file2copy)
  ;RunProgram("Wordpad")
EndIf

            

Posted: Wed Oct 31, 2007 10:07 pm
by Marco2007
:shock: :!: :!: :!: :!: :!: :shock:

Thanx a lot, sparkie!!! It works with Word 2007!! :D

Posted: Thu Nov 01, 2007 1:17 am
by Sparkie
You're welcome....... I guess we got lucky with that one 8)

Posted: Thu May 29, 2008 8:51 pm
by Marco2007
Hi Sparkie,

sorry for being nasty again. :oops:

Do you know, what I have to do to get this one work

Code: Select all

CopyFileFromClipboard(target.s)
?

thanx
marco

Posted: Thu May 29, 2008 10:27 pm
by akj
Here's another way to do it:

Code: Select all

Procedure CopyFile2ClipBoard(file.s)
Protected clipFile = 0
Protected *mem, fil
Protected fez = FileSize(file)
If fez>0
  *mem = AllocateMemory(fez+2)
  fil = ReadFile(#PB_Any, file)
  clipfile = ReadData(fil, *mem, fez)
  CloseFile(fil)
  SetClipboardText(PeekS(*mem, fez))
  FreeMemory(*mem)
EndIf
ProcedureReturn clipfile
EndProcedure

Define file2copy.s
file2copy = OpenFileRequester("Select file to copy to the ClipBoard", "", "All files|*.*", 0)
If file2copy
  CopyFile2ClipBoard(file2copy)
  RunProgram("Wordpad")
EndIf
End

Posted: Mon Jun 02, 2008 1:17 pm
by ar-s
Thanks for that cool procedure :)

Posted: Mon Apr 13, 2009 1:18 pm
by PB
> Here's another way to do it

Sorry, akj, but your way doesn't work for me. I copy an exe file with it,
but I can't paste it in Explorer like Sparkie's code does. Don't know why.

Posted: Mon Apr 13, 2009 1:27 pm
by rsts
PB wrote:> Here's another way to do it

I can't paste it in Explorer like Sparkie's code does. Don't know why.
maybe setClipboardText has something to do with it?

Peeks isn't going to handle an exe.

cheers