2014-01-15 58 views
0

我很難實現一個重要的功能,它需要數據在複製時從辦公室剪貼板中清除。清除辦公室剪貼板,以便複製的數據不會與其他辦公室程序共享

意圖不是與其他辦公室程序共享內容,例如, word,powerpoint等等。這個場景是我的excel表格中有一些重要的內容。一旦我製作了它的副本,它很快就可以在辦公室剪貼板上找到。如果我繼續在Excel中複製這些東西,它會一直收集到其他辦公室程序中。但是,Windows剪貼板將只包含最近的輸入可以清除使用

System.Windows.Forms.Clipboard.clear(): 

有沒有辦法走出來清除Office剪貼板?

我搜索了一下,發現程序中可能沒有明確的解決方案,但在FindWindowEx(....)的幫助下獲取辦公剪貼板窗口應該是可能的,然後可以發送消息以便清除內容。看來我無法以這種方式得到它。

有人可以告訴他們是否遇到同樣的問題?

回答

0

這可能給你一個正確的方向輕推...摘自:mrexcel.com

Option Explicit 

Sub myClr() 
    'Put this sub inta a Sheet Module, like: Sheet1. 
    Call ClearOfficeClipboard 
End Sub 

'Put the code from here down into a Standard Module, like Module1. 
Private Declare Function FindWindowEx Lib "user32.dll" _ 
    Alias "FindWindowExA" (ByVal hWnd1 As Long, _ 
    ByVal hWnd2 As Long, ByVal lpsz1 As String, _ 
    ByVal lpsz2 As String) As Long 

Private Declare Function PostMessage Lib "user32.dll" Alias _ 
    "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _ 
    ByVal wParam As Long, ByVal lParam As Long) As Long 

Private Const WM_LBUTTONDOWN As Long = &H201& 
Private Const WM_LBUTTONUP As Long = &H202& 

'creates a long variable out of two words 
Private Function MakeLong(ByVal nLoWord As Integer, ByVal nHiWord As Integer) As Long 
    MakeLong = nHiWord * 65536 + nLoWord 
End Function 


Sub ClearOfficeClipboard() 
    Dim hMain&, hExcel2&, hWindow&, hParent& 
    Static sTask As String 
    '****Dim hClip As Long************************'changed by Lary 
    Dim octl, bScreenUpdatingIsOn As Boolean 
    Static lParameter As Long, bNotFirstVisibleTime As Boolean, hClip As Long, bNotFirstTime As Boolean 

If Not (bNotFirstTime) Then 
    lParameter = MakeLong(120, 18) 
    sTask = Application.CommandBars("Task Pane").NameLocal 
    'Handle for XLMAIN 
    hMain = Application.hwnd 
    bNotFirstTime = True 
End If 

With Application.CommandBars("Task Pane") 

    If Not .Visible Then 
     'assume have to force the window if it is not visible, since it appears that 
     ' the window class does not remain loaded if you clear a non-visible clipboard 


      'determine current status of screenupdating so that this sub does not change it 
      bScreenUpdatingIsOn = Application.ScreenUpdating 
      If bScreenUpdatingIsOn Then Application.ScreenUpdating = False 

      Set octl = Application.CommandBars(1).FindControl(ID:=809, recursive:=True) 
      If Not octl Is Nothing Then octl.Execute 
      .Visible = False 

      'return to screenupdating on if that is what it was in the beginning 
      If bScreenUpdatingIsOn Then Application.ScreenUpdating = True 

      If hClip = 0 Then 
       hParent = hMain: hWindow = 0 
       hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString) 
       If hWindow Then 
        hParent = hWindow: hWindow = 0 
        hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", vbNullString) 
       End If 
      End If 
    Else 
     If Not (bNotFirstVisibleTime) Then** 'find hClip if window is visible 
      Do 
       hExcel2 = FindWindowEx(hMain, hExcel2, "EXCEL2", vbNullString) 

       hParent = hExcel2: hWindow = 0 
       hWindow = FindWindowEx(hParent, hWindow, "MsoCommandBar", sTask) 
       If hWindow Then 
        hParent = hWindow: hWindow = 0 
        hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString) 
        If hWindow Then 
         hParent = hWindow: hWindow = 0 
         hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", vbNullString) 
         If hClip > 0 Then 
          Exit Do 
         End If 
        End If 
       End If 
      Loop While hExcel2 > 0 
      bNotFirstVisibleTime = True 
     End If 
    End If 
End With 

If hClip = 0 Then 
    MsgBox "Cant find Clipboard window" 
    Exit Sub 
End If 
Call PostMessage(hClip, WM_LBUTTONDOWN, 0&, lParameter) 
Call PostMessage(hClip, WM_LBUTTONUP, 0&, lParameter) 
End Sub 
+0

我看到了這個解決方案,但無法弄清楚它是否僅適用於特定版本的MS Office。因爲它不適用於Office 2007。 –

0

下面的代碼已經在vb.net被定製爲Excel 2013。只需在功能區中添加一個按鈕,代碼就像魅力一樣。

Private Const WM_LBUTTONDOWN As Long = &H201& 
     Private Const WM_LBUTTONUP As Long = &H202& 

    WithEvents oAppWD As Excel.Application 

    Public oDoc As Excel.Workbook 

    Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Int32) 
    Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Int32, ByVal hWnd2 As Int32, ByVal lpsz1 As String, ByVal lpsz2 As String) As Int32 
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32 
    Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hWnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32 
    Declare Function BringWindowToTop Lib "user32" (ByVal hWnd As Int32) As Int32 



Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click 
     Dim hMain As Int32, hWord As Int32, hClip As Int32, hWindow As Int32, hParent As Int32 
     Dim lParameter As Int32 
     Dim sTask As String 
     Dim HWND As Int32 

     'Open the selected File 
     oAppWD = Globals.ThisAddIn.Application 'DirectCast(System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"), Excel.Application) 
     oAppWD.Visible = True 
     oDoc = oAppWD.ActiveWorkbook 

     oDoc.Activate() 
     oDoc.Windows(1).Activate() 


     Sleep(2000) 
     ' MessageBox.Show("Doing it.....") 

     HWND = FindWindow("XLMAIN", vbNullString) 

     ' Make Office Clipboard Visible 
     oAppWD.CommandBars("Office Clipboard").Visible = True 

     BringWindowToTop(HWND) 

     ' Get the handles of the respective Windows Of the Office 
     sTask = "Office Clipboard" 
     hMain = HWND 
     hWord = FindWindowEx(hMain, 0, "EXCEL2", vbNullString) 

     hParent = hWord : hWindow = 0 
     hWindow = FindWindowEx(hParent, 0, "MsoCommandBar", sTask) 
     If hWindow Then 
      hParent = hWindow : hWindow = 0 
      hWindow = FindWindowEx(hParent, 0, "MsoWorkPane", vbNullString) 
      If hWindow Then 
       hParent = hWindow : hWindow = 0 

       hClip = FindWindowEx(hParent, 0, vbNullString, "Collect and Paste 2.0") 

      End If 
     End If 


     If hClip = 0 Then 
      MsgBox("Cant find Clipboard window") 
      Exit Sub 
     End If 
     ' Pass the message 120,18 are the respective co-ordinates of the Clear all button. 
     lParameter = MakeLong(120, 18) 
     ' Send the Message 
     Call PostMessage(hClip, WM_LBUTTONDOWN, 0&, lParameter) 
     Call PostMessage(hClip, WM_LBUTTONUP, 0&, lParameter) 
     Sleep(100) 

    End Sub 
    Private Function MakeLong(ByVal nLoWord As Integer, ByVal nHiWord As Integer) As Int32 
     MakeLong = nHiWord * 65536 + nLoWord 
    End Function