Results 1 to 6 of 6

Thread: [Word, Excel, PowerPoint] add mouseclick event handler

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    17

    [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.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Word, Excel, PowerPoint] add mouseclick event handler

    Either it is not possible or it is extremely difficult I guess.
    i think you would have to sub class the window and process /edit the system window messages, not from within the office application, i am sure quite possible, but not easy, above my grade unless i researched from scratch, but i am still in the dark ages of vb6

    there would be plenty of subclassing examples available if you do a search on subclassing excel (or whichever application) or maybe your webbrowser as the parent window
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    17

    Re: [Word, Excel, PowerPoint] add mouseclick event handler

    Although eventually this does have to work in VB6, as you can see from my code I'm using .NET. At the moment I'm building an Interop UserControl in VB .NET and integrating it into my legacy VB6 app. It seems to work well this way and I get the power of .NET.
    I agree that sub-classing seems like a good possibility but I haven't figured out how or what yet.

    Does this forum only pertain to VB6? I thought there was another forum here for that.

  4. #4
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: [Word, Excel, PowerPoint] add mouseclick event handler

    Word 2010 adds a series of new Application-level events, allowing
    you to react to events surrounding the use of protected-view windows.
    Whether you open a document from the internet, or select to open a local
    document in protected view, the following events can occur as you
    interact with the protected-view document:

    ProtectedViewWindowActivate
    ProtectedViewWindowBeforeClose
    ProtectedViewWindowBeforeEdit
    ProtectedViewWIndowDeactivate
    ProtectedViewWindowOpen
    ProtectedViewWindowSize

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Word, Excel, PowerPoint] add mouseclick event handler

    Does this forum only pertain to VB6? I thought there was another forum here for that.
    no this is for a mix relating to office development including .net, most VBA code is a subset of vb6 and application related functions/ methods /properties
    i just pointed out that i only use vb6 and vba and am not versed in .net, though can read most of what is happening

    also i had no idea about the information posted by 3com, as i also use really old office version, perhaps it can help you
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: [Word, Excel, PowerPoint] add mouseclick event handler


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
  •  



Click Here to Expand Forum to Full Width