Sunday 4 November 2018

IE is also an HTML editor didn't y'all know? MSHTML Editing

Whilst researching for these articles I read and surf a lot. Sometimes I bump into something very surprising, in this article a setting that turns Internet Explorer into an HTML editor. Sadly, there is no formatting toolbar to make it a complete application. I can't think of a use case for this just now but I must document this whilst I remember it. After all IE is being phased out so expect the documentation to fade.

The precise technical term for this feature is MSHTML Editing. It seems IE could be used as a giant ActiveX control that developers could incorporate into their application. Enjoy!

Update: I use this technique in preparation of my blog posts to see if code can be cut and paste successfully with no transpose glitches.

Links

Option Explicit

'* Tools - References
'*      MSHTML      Microsoft HTML Object Library                   C:\Windows\SysWOW64\mshtml.tlb
'*      SHDocVw     Microsoft Internet Controls                     C:\Windows\SysWOW64\ieframe.dll
'*      Shell32     Microsoft Shell Controls And Automation         C:\Windows\SysWOW64\shell32.dll

Sub Test()

    '*
    '* Brought to you by the Excel Development Platform Blog
    '* https://exceldevelopmentplatform.blogspot.com
    '*

    Dim objIE As Object
    Set objIE = VBA.CreateObject("InternetExplorer.Application")

    objIE.Visible = True

    Dim sNav As String
    sNav = "https://technet.microsoft.com/en-us/aa753622(v=vs.71)"


    objIE.Navigate sNav

    Application.Wait Now() + CDate("00:00:03")

    Dim objIE2 As Object
    Set objIE2 = ReacquireInternetExplorer(sNav)

    While objIE2.Busy
        DoEvents
    Wend

    'Stop
    Dim doc As Object
    Set doc = objIE2.Document

    If doc.DesignMode = "On" Then
        doc.DesignMode = "Off"
    Else
        doc.DesignMode = "On"
        AppActivate Application.Caption
        MsgBox "IE is now editable, select elements and drag, type and delete text.   It is a pity there is no formatting toolbar. Enjoy!"
    End If

End Sub

Private Function ReacquireInternetExplorer(ByVal sMatch As String) As Object
    Dim oShell As Shell32.Shell: Set oShell = New Shell32.Shell
    Dim wins As Object: Set wins = oShell.Windows
    Dim winLoop As Variant
    For Each winLoop In oShell.Windows
        If "C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE" = winLoop.FullName Then
            If StrComp(sMatch, winLoop.LocationURL, vbTextCompare) = 0 Then
                Set ReacquireInternetExplorer = winLoop.Application
                GoTo SingleExit
            Else
                Dim sFile2 As String
                sFile2 = "file:///" & VBA.Replace(sMatch, "", "/")
                If StrComp(sFile2, winLoop.LocationURL, vbTextCompare) = 0 Then
                    Set ReacquireInternetExplorer = winLoop.Application
                    GoTo SingleExit
                End If
            End If
        End If
    Next
SingleExit:
End Function

No comments:

Post a Comment