Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Win API / July 2008



Tip: Looking for answers? Try searching our database.

How to copy Clipboard to file?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michael Mueller - 30 Jun 2008 07:10 GMT
Hi.

Within Excel I added an object containing a file.
I can bring this file to the clipboard without any problems.

But how can I store this file from the clipboard to the local drive using
code?
Using paste (Ctrl+V) in explorer works fine, but how to realize this with
some lines of code?

Thanks
Michi
Michael Mueller - 30 Jun 2008 10:33 GMT
> Within Excel I added an object containing a file.
> I can bring this file to the clipboard without any problems.
[quoted text clipped - 3 lines]
> Using paste (Ctrl+V) in explorer works fine, but how to realize this with
> some lines of code?

While trying some API-calls if discovered the following call:
   IsClipboardFormatAvailable
which is 3 for my file.
It's 15 when doing Ctrl-C in the Explorer.

Due to being a file in both times, a help how to handle the normal file
within the clipboard would be much apprechiated, too.

Michi
Dave O. - 01 Jul 2008 10:34 GMT
Your question is not clear, when you say "file from the clipboard" do you
mean that the clipboard contains the data or a reference to the file name?
To put it another way, is it as if you selected some text in notepad or
similar and pressed Ctrl+C or is it as if you selected a file in Windows
Explorer and pressed Ctrl+C
If it's the former then you can do everything with the Clipboard object
built into VB6, if you are dealing with actual files then it gets a LOT more
interesting.

Regards
Dave O.

> Hi.
>
[quoted text clipped - 8 lines]
> Thanks
> Michi
Michael Mueller - 01 Jul 2008 12:54 GMT
Hi Dave.

> Your question is not clear, when you say "file from the clipboard" do you
> mean that the clipboard contains the data or a reference to the file name?

It doesn't contain any references (as far as I can see).

What I do:
In Excel I do Insert-Object-Create from file, no linking.

Now when I copy this object i can Ctrl-V-paste in within the explorer and
the file will be copied/created.

What I wanna do is to do this paste using code.
But the clipboardformat is not a file (IsClipboardFormatAvailable=15) like
copy a file directly wihtin Explorer, but being an embedded Object
(IsClipboardFormatAvailable=3).

> If it's the former then you can do everything with the Clipboard object
> built into VB6, if you are dealing with actual files then it gets a LOT
> more interesting.

How would this code look like?

Thanks
Michi
Dave O. - 01 Jul 2008 13:35 GMT
> Hi Dave.
>
[quoted text clipped - 23 lines]
> Thanks
> Michi

Declarations (Watch out for line breaks)

Private Const GMEM_MOVEABLE = &H2
Private Const GMEM_ZEROINIT = &H40
Private Const CF_HDROP = 15
Private Const GET_DROP_COUNT = &HFFFFFFFF
Private Const MAX_PATH = 260

Private  Type POINTAPI
 x As Long
 y As Long
End Type

Private Type DROPFILES
 pFiles As Long
 pt     As POINTAPI
 fNC    As Long
 fWide  As Long
End Type

Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As
Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long,
ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hmem As Long) As
Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hmem As Long) As
Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As
Long, ByVal hmem As Long) As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As
Long) As Long
Private Declare Function DragQueryFile Lib "shell32.dll" Alias
"DragQueryFileA" (ByVal hDrop As Long, ByVal UINT As Long, ByVal lpStr As
String, ByVal ch As Long) As Long

To add a single file to the clipboard this works:

Private Sub FileToClipboard(picFile as String)
' Add a file to the clipboard, this is the easiest way to do it - honestly!
Dim df       As DROPFILES
Dim lpGlobal As Long
Dim s        As String
Dim r        As Long
Dim txtPtr   As Long

s = picFile & vbNullChar & vbNullChar
df.pFiles = Len(df)
r = df.pFiles + Len(s)
OpenClipboard Me.hWnd
EmptyClipboard
txtPtr = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, r)
lpGlobal = GlobalLock(txtPtr)
Call CopyMemory(ByVal lpGlobal, df, Len(df))
Call CopyMemory(ByVal (lpGlobal + Len(df)), ByVal s, Len(s))
Call GlobalUnlock(txtPtr)
Call SetClipboardData(15, txtPtr)
CloseClipboard
End Sub

For multiple files a NULL seperated list should do the job

To get the files from the clipboard, this is part of a routine I use to add
the list to a ListView (LV):

Private Function ShowFileList() As Integer
Dim lHandle   As Long
Dim strBuffer As String
Dim lCount    As Long
Dim s         As String

LV.ListItems.Clear

If OpenClipboard(Me.hWnd) > 0 Then

 lHandle = GetClipboardData(CF_HDROP)
 If Not lHandle = 0 Then
   strBuffer = String(MAX_PATH, vbNullChar)
   For lCount = 0 To DragQueryFile(lHandle, GET_DROP_COUNT, strBuffer,
Len(strBuffer)) - 1
     s = Left$(strBuffer, DragQueryFile(lHandle, lCount, strBuffer,
Len(strBuffer)))
     LV.ListItems.Add , , s
   Next
 End If

This is nowhere near as simple as dealing with text or images in the
clipboard, but should not be too tricky. There are a few unintuitive aspects
and as I have not done much of this sort of thing for years I might not be
as much help as I could be, you may find that now I pointed you in the right
direction you can use Google to get any further instruction you may need.

Regards
Dave O.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.