|
-
Nov 18th, 2013, 12:54 AM
#1
Thread Starter
Junior Member
[Word, Excel, PowerPoint] add mouseclick event handler
I would like to display a Word document in a WebBrowser control disabling almost all user interaction - no default function key behavior, accelerator key behavior - and also have no GUI components - menus, ribbons etc. Users should only be able to select text and copy to clipboard - kind of like a PDF "image" of a document. So far I have been able to get pretty close to this behavior. However, I need one more critical bit of functionality: I must be able to react to mouse clicks and key down events.
Is there a way to add/override/customize the default mouse and key events for a Word document?
Here is what I've done so far.
Code:
Private Sub wbrPreview_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles wbrPreview.DocumentCompleted
Dim objDocument As Object
'MessageBox.Show("DocumentCompleted")
If e.Url.ToString.EndsWith(".html") Or e.Url.ToString.EndsWith(".htm") Then
Dim htmlDocument As HtmlDocument = wbrPreview.Document
'Attach KeyDown event handler
AddHandler htmlDocument.Body.KeyUp, New HtmlElementEventHandler(AddressOf wbrPreview_KeyUp)
'Attach Click event handler
AddHandler htmlDocument.Body.Click, New HtmlElementEventHandler(AddressOf wbrPreview_Click)
ElseIf e.Url.ToString.EndsWith(".docx") Or e.Url.ToString.EndsWith(".doc") Then
Dim objIE As InternetExplorer = wbrPreview.ActiveXInstance
objIE.ExecWB(OLECMDID.OLECMDID_HIDETOOLBARS, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER)
objDocument = objIE.Document
Dim wordDocument As Word.Document = objDocument
wordDocument.Application.ActiveDocument.Protect(3)
wordDocument.ActiveWindow.View.Zoom.PageFit = 2
ElseIf e.Url.ToString.EndsWith(".xlsx") Or e.Url.ToString.EndsWith(".xls") Then
Dim objIE As InternetExplorer = wbrPreview.ActiveXInstance
objDocument = objIE.Document
Dim excelDocument As Excel.Workbook = objDocument
For Each excelWorksheet As Excel.Worksheet In excelDocument.Worksheets
excelWorksheet.Protect()
Next
End If
wbrPreview.Visible = True
wbrPreview.Focus()
blnOverHyperlink = False
End Sub
As you can see the logic of using a container document has proven useful to me with respect to HTML documents. As the code below shows I am able to get access to the HTML document in the WebBrowser control once it has completed loading. At that point I am able to make use of the AddHandler VB statement to associate an event with a handler at runtime. In this manner I can solve my problem for HTML document by customizing the event handler code. And it works pretty reliably so far. What I need is a way to do the same thing for Word, Excel and PowerPoint documents. However, unlike the HtmlDocument class, the Office Interop documents don't seem to expose a KeyUp or Click or MouseClick event.
It's hard to imagine a more straightforward question than the one I'm posing and yet there have been no responses elsewhere on the interweb . I don't know what to make of this. Either it is not possible or it is extremely difficult I guess.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|