2016-09-27 23 views
0

我不能相信這是很難找到(或未來),但我想寫一個宏,將複製文件(word,pdf,txt)在預定義的路徑,然後我可以在文件夾中用鼠標右鍵單擊並粘貼。 所以複製到windows剪貼板。Excel VBA複製文件,但不是粘貼

+0

澄清:你想有一個宏將複製一個文件對象(而不是其內容),但不會實際執行粘貼併爲您離開該任務? – Dave

+0

是的,它已被回覆 –

回答

0

試試這個方法: https://social.msdn.microsoft.com/Forums/en-US/e624729a-e8bd-4d16-867f-6bd48000bbaa/copy-file-into-clipboard?forum=isvvba

您需要定義在最後面的路徑:

afile(0) = "c:\bdlog.txt" 'The file actually exists 

代碼嘗試:

Option Explicit 

' Required data structures 
Private Type POINTAPI 
x As Long 
y As Long 
End Type 

' Clipboard Manager Functions 
Private Declare Function EmptyClipboard Lib "user32"() As Long 
Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long 
Private Declare Function CloseClipboard Lib "user32"() 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 IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long 

' Other required Win32 APIs 
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 
Private Declare Function DragQueryPoint Lib "shell32.dll" (ByVal hDrop As Long, lpPoint As POINTAPI) As Long 
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long 
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem 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 Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) 

' Predefined Clipboard Formats 
Private Const CF_TEXT = 1 
Private Const CF_BITMAP = 2 
Private Const CF_METAFILEPICT = 3 
Private Const CF_SYLK = 4 
Private Const CF_DIF = 5 
Private Const CF_TIFF = 6 
Private Const CF_OEMTEXT = 7 
Private Const CF_DIB = 8 
Private Const CF_PALETTE = 9 
Private Const CF_PENDATA = 10 
Private Const CF_RIFF = 11 
Private Const CF_WAVE = 12 
Private Const CF_UNICODETEXT = 13 
Private Const CF_ENHMETAFILE = 14 
Private Const CF_HDROP = 15 
Private Const CF_LOCALE = 16 
Private Const CF_MAX = 17 

' New shell-oriented clipboard formats 
Private Const CFSTR_SHELLIDLIST As String = "Shell IDList Array" 
Private Const CFSTR_SHELLIDLISTOFFSET As String = "Shell Object Offsets" 
Private Const CFSTR_NETRESOURCES As String = "Net Resource" 
Private Const CFSTR_FILEDESCRIPTOR As String = "FileGroupDescriptor" 
Private Const CFSTR_FILECONTENTS As String = "FileContents" 
Private Const CFSTR_FILENAME As String = "FileName" 
Private Const CFSTR_PRINTERGROUP As String = "PrinterFriendlyName" 
Private Const CFSTR_FILENAMEMAP As String = "FileNameMap" 

' Global Memory Flags 
Private Const GMEM_FIXED = &H0 
Private Const GMEM_MOVEABLE = &H2 
Private Const GMEM_NOCOMPACT = &H10 
Private Const GMEM_NODISCARD = &H20 
Private Const GMEM_ZEROINIT = &H40 
Private Const GMEM_MODIFY = &H80 
Private Const GMEM_DISCARDABLE = &H100 
Private Const GMEM_NOT_BANKED = &H1000 
Private Const GMEM_SHARE = &H2000 
Private Const GMEM_DDESHARE = &H2000 
Private Const GMEM_NOTIFY = &H4000 
Private Const GMEM_LOWER = GMEM_NOT_BANKED 
Private Const GMEM_VALID_FLAGS = &H7F72 
Private Const GMEM_INVALID_HANDLE = &H8000 
Private Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT) 
Private Const GPTR = (GMEM_FIXED Or GMEM_ZEROINIT) 

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


Public Function ClipboardCopyFiles(Files() As String) As Boolean 

Dim data As String 
Dim df As DROPFILES 
Dim hGlobal As Long 
Dim lpGlobal As Long 
Dim i As Long 

' Open and clear existing crud off clipboard. 
If OpenClipboard(0&) Then 
Call EmptyClipboard 

' Build double-null terminated list of files. 
For i = LBound(Files) To UBound(Files) 
data = data & Files(i) & vbNullChar 
Next 
data = data & vbNullChar 

' Allocate and get pointer to global memory, 
' then copy file list to it. 
hGlobal = GlobalAlloc(GHND, Len(df) + Len(data)) 
If hGlobal Then 
lpGlobal = GlobalLock(hGlobal) 

' Build DROPFILES structure in global memory. 
df.pFiles = Len(df) 
Call CopyMem(ByVal lpGlobal, df, Len(df)) 
Call CopyMem(ByVal (lpGlobal + Len(df)), ByVal data, Len(data)) 
Call GlobalUnlock(hGlobal) 

' Copy data to clipboard, and return success. 
If SetClipboardData(CF_HDROP, hGlobal) Then 
ClipboardCopyFiles = True 
End If 
End If 

' Clean up 
Call CloseClipboard 
End If 

End Function 

Public Function ClipboardPasteFiles(Files() As String) As Long 

Dim hDrop As Long 
Dim nFiles As Long 
Dim i As Long 
Dim desc As String 
Dim filename As String 
Dim pt As POINTAPI 
Const MAX_PATH As Long = 260 

' Insure desired format is there, and open clipboard. 
If IsClipboardFormatAvailable(CF_HDROP) Then 
If OpenClipboard(0&) Then 

' Get handle to Dropped Filelist data, and number of files. 
hDrop = GetClipboardData(CF_HDROP) 
nFiles = DragQueryFile(hDrop, -1&, "", 0) 

' Allocate space for return and working variables. 
ReDim Files(0 To nFiles - 1) As String 
filename = Space(MAX_PATH) 

' Retrieve each filename in Dropped Filelist. 
For i = 0 To nFiles - 1 
Call DragQueryFile(hDrop, i, filename, Len(filename)) 
Files(i) = TrimNull(filename) 
Next 

' Clean up 
Call CloseClipboard 
End If 

' Assign return value equal to number of files dropped. 
ClipboardPasteFiles = nFiles 
End If 

End Function 

Private Function TrimNull(ByVal sTmp As String) As String 

Dim nNul As Long 

' 
' Truncate input sTmpg at first Null. 
' If no Nulls, perform ordinary Trim. 
' 
nNul = InStr(sTmp, vbNullChar) 
Select Case nNul 
Case Is > 1 
TrimNull = Left(sTmp, nNul - 1) 
Case 1 
TrimNull = "" 
Case 0 
TrimNull = Trim(sTmp) 
End Select 

Sub maaa() 

'i = "c:\" & ActiveDocument.Name 
'ActiveDocument.SaveAs i 
Dim afile(0) As String 

afile(0) = "c:\070206.excel" 'The file actually exists 
MsgBox ClipboardCopyFiles(afile) 
End Sub 
+0

請提供您的鏈接的相關信息。如果鏈接死亡,這個答案對未來的用戶來說不是很有價值。 – Comintern

+0

是的,我明白了。我有複製/粘貼有趣的部分 –

+0

謝謝,這正是我需要的,我不能相信它的複雜做一個簡單的任務,如:) –