Lets Learn

Opinion Matters

Clear Office Clipboard Programmatically

Posted by Ankush on March 1, 2011


Lets say you are automating Word and looking for a way to clear an Office Clipboard.

image

Have a look in the left of the screen shot and it will show you the Office Clipboard. So you look into the Office Object Modal but there is no method/property to clear the clipboard.

Any solutions? Our old friend, win32 API come for the rescue.

Here is the solution:

Step 1: Declare the namespaces

   1: Imports Word = Microsoft.Office.Interop.Word

   2: Imports System.Runtime.InteropServices

 

Step 2:

Declare the local variables and APIs

   1: Private Const WM_LBUTTONDOWN As Long = &H201&

   2:    Private Const WM_LBUTTONUP As Long = &H202&

   3:

   4:    ' Variables to hold Word Objects

   5:    WithEvents oAppWD As Word.Application

   6:    Public oDoc As Word.Document

   7:

   8:    ' API to be used

   9:    Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Int32)

  10:    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

  11:    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32

  12:    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

  13:    Declare Function BringWindowToTop Lib "user32" (ByVal hWnd As Int32) As Int32

Step 3: Actual code

   1: Private Function MakeLong(ByVal nLoWord As Integer, ByVal nHiWord As Integer) As Int32

   2:        MakeLong = nHiWord * 65536 + nLoWord

   3:    End Function

   4:

   5:

   6:    Private Sub btnClearWord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearWord.Click

   7:        Dim hMain As Int32, hWord As Int32, hClip As Int32, hWindow As Int32, hParent As Int32

   8:        Dim lParameter As Int32

   9:        Dim sTask As String

  10:        Dim HWND As Int32

  11:

  12:        'Open the selected File

  13:        oAppWD = DirectCast(System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application"), Word.Application)

  14:        oAppWD.Visible = True

  15:        oDoc = oAppWD.ActiveDocument

  16:

  17:        oAppWD.Activate()

  18:        oDoc.Activate()

  19:        oDoc.ActiveWindow.SetFocus()

  20:        oDoc.ActiveWindow.Activate()

  21:

  22:        Sleep(2000)

  23:        MessageBox.Show("Doing it.....")

  24:

  25:        HWND = FindWindow("OpusApp", vbNullString)

  26:

  27:        ' Make Office Clipboard Visible

  28:        oAppWD.CommandBars("Office Clipboard").Visible = True

  29:

  30:        BringWindowToTop(HWND)

  31:

  32:        ' Get the handles of the respective Windows Of the Office 

  33:        sTask = "Office Clipboard"

  34:        hMain = HWND

  35:        hWord = FindWindowEx(hMain, 0, "MsoCommandBarDock", vbNullString)

  36:

  37:        hParent = hWord : hWindow = 0

  38:        hWindow = FindWindowEx(hParent, 0, "MsoCommandBar", sTask)

  39:        If hWindow Then

  40:            hParent = hWindow : hWindow = 0

  41:            hWindow = FindWindowEx(hParent, 0, "MsoWorkPane", vbNullString)

  42:            If hWindow Then

  43:                hParent = hWindow : hWindow = 0

  44:                '   hClip = FindWindowEx(hParent, 0, "bosa_sdm_Microsoft Office Word 12.0", vbNullString)

  45:                hClip = FindWindowEx(hParent, 0, "bosa_sdm_msword", vbNullString)

  46:

  47:

  48:            End If

  49:        End If

  50:

  51:

  52:        If hClip = 0 Then

  53:            MsgBox("Cant find Clipboard window")

  54:            Exit Sub

  55:        End If

  56:        ' Pass the message 120,18 are the respective co-ordinates of the Clear all button.

  57:        lParameter = MakeLong(120, 18)

  58:        ' Send the Message

  59:        Call PostMessage(hClip, WM_LBUTTONDOWN, 0&, lParameter)

  60:        Call PostMessage(hClip, WM_LBUTTONUP, 0&, lParameter)

  61:        Sleep(100)

  62:

  63:    End Sub

Try this and let me know how it goes for you Smile

Advertisements

2 Responses to “Clear Office Clipboard Programmatically”

  1. Nick said

    I’ve been looking for a way of doing exactly this ever since upgrading to Office 2010 a couple of months back, but don’t have the technical knowledge to understand whether it should be possible to implement this solution in Word VBA. Can you offer any further guidance ?

    Nick

  2. Nick said

    I’ve been trying to find out how to do exactly this since upgrading recently to Office 2010. With my limited knowledge of VBA, I can’t figure out how to implement your approach in Word VBA (or indeed if this would be possible; better still from Excel). Could you provide any further guidance?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: