Lets say you are automating Word and looking for a way to clear an Office Clipboard.
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 ![]()