电脑excel文件怎样发送

发布时间: 2023-04-15 18:44 阅读: 文章来源:转载

思路与之前发送信息一样,将“文件”放入剪贴板中,就可以用CTRL+V,粘贴到微信信息框中实现发送文件了。

表格式样

可是VBA 自带函数FileCopy 并不产生文件复制到剪贴板的效果,需调用Window API接口操作,代码如下。(来自EXCEL HOME论坛)

Private declare PtrSafe Function RegisterClipboardFormat Lib "user32.dll" Alias "RegisterClipboardFormatW" (ByVal lpString As LongPtr) As LongPrivate declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hWnd As LongPtr) As LongPrivate declare PtrSafe Function CloseClipboard Lib "user32" () As LongPrivate declare PtrSafe Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As LongPtr) As LongPrivate declare PtrSafe Function EmptyClipboard Lib "user32" () As LongPrivate declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As LongPtr) As LongPtrPrivate declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As LongPtr) As LongPrivate declare PtrSafe Function GlobalAlloc Lib "kernel32" (ByVal flags As Long, ByVal Size As Long) As LongPtrPrivate declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal hpvDest As LongPtr, ByVal hpvSource As LongPtr, ByVal cbCopy As Long)‘API函数定义结束Private Const CF_HDROP As Long = 15&Private Const DROPEFFECT_COPY As Long = 1Private Const DROPEFFECT_MOVE As Long = 2Private Const GMEM_ZEROINIT As Long = &H40Private Const GMEM_MOVEABLE As Long = &H2Private Const GMEM_DDESHARE As Long = &H2000‘结构定义开始Private Type POINTAPIX As LongY As LongEnd TypePrivate Type dropFilespFiles As Longpt As POINTAPIfNC As LongfWide As LongEnd Type‘结构定义结束Public declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

上面的代码都是 API函数的定义所需的(64位的VBE环境)。下面代码是将文件复制到剪贴板。

Sub clipCopyFile(FileList As Variant)Dim uDropEffect As Long, I As LongDim dropFilesAs dropFilesDim uGblLen As Long, uDropFilesLenAs LongDim hGblFilesAs LongPtrDim hGblEffectAs LongPtrDim mPtrAs LongPtrDim FNameAs StringIf OpenClipboard(0) ThenEmptyClipboardFName = Trim(FileList)If Len(FName) ThenuDropEffect = RegisterClipboardFormat(StrPtr("Preferred DropEffect"))hGblEffect = GlobalAlloc(GMEM_ZEROINIT Or GMEM_MOVEABLE Or GMEM_DDESHARE, Len(uDropEffect))mPtr = GlobalLock(hGblEffect)I = DROPEFFECT_COPYCopyMemory mPtr, VarPtr(I), Len(I)GlobalUnlock hGblEffectSetClipboardData uDropEffect, hGblEffectuDropFilesLen = LenB(dropFiles)With dropFiles.pFiles = uDropFilesLen.fWide = CLng(True)End WithuGblLen = uDropFilesLen + LenB(FName) + 8hGblFiles = GlobalAlloc(GMEM_ZEROINIT Or GMEM_MOVEABLE Or GMEM_DDESHARE, uGblLen)mPtr = GlobalLock(hGblFiles)CopyMemory mPtr, VarPtr(dropFiles), uDropFilesLenmPtr = mPtr + uDropFilesLenhGblEffect = StrPtr(FName)I = LenB(FName)CopyMemory mPtr, hGblEffect, IGlobalUnlock hGblFilesSetClipboardData CF_HDROP, hGblFilesEnd IfCloseClipboardEnd IfEnd Sub

此clipCopyFile过程只处理单个的文件(已被我修改了),需要一次处理多个文件的,可去EXCEL HOME论坛查看源码。

微信发文件代码:

Sub 发文件()Set ws = CreateObject("wscript.shell")ws.SendKeys "^%w"For I = 2 To Cells(Rows.Count, 1).End(xlUp).Rowws.Run "mshta vbscript:ClipboardData.SetData(""Text""," & Chr(34) & Cells(I, 1) & Chr(34) & ")(close)", 0, TrueSleep 300ws.SendKeys "^f"Sleep 1000ws.SendKeys "^v"Sleep 500ws.SendKeys "{ENTER}"Sleep 500ws.Run "mshta vbscript:ClipboardData.SetData(" & Chr(34) & "Text" & Chr(34) & "," & Chr(34) & Cells(I, 2) & Chr(34) & ")(close)", 0, TrueSleep 500ws.SendKeys "^v"Sleep 500ws.SendKeys "{ENTER}"wjName = Cells(I, 3).ValueclipCopyFile wjNamews.SendKeys "^v"Sleep 500ws.SendKeys "{ENTER}"Next ISet ws = NothingEnd Sub
•••展开全文
相关文章