Click here to Skip to main content
Click here to Skip to main content

Using ExecWB with the native .NET 2.0 WebBrowser control

By , 17 Jun 2008
 

Introduction

One of the most annoyingly interesting components within the VB.NET 2008 environment is the WebBrowser control (or class, if you prefer) that is bundled as standard. It exposes a number of common methods and properties, but lacks one of the most useful elements that can be found in the full-blown web browser (a.k.a. SHDocVw or Ieframe.dll) version, namely the ExecWB command. The following class provides a simple example of how to harness the power of ExecWB from the System.Windows.Forms.WebBrowser class.

Background

This solution came about as I researched using the WebBrowser control and stumbled into the complex world of variations on a theme. Why all of this power couldn't be harnessed in a single solution is beyond me. However, my mission is to continue simplifying a unified theory of everything (relating to the Microsoft WebBrowser).

Using the code

  1. Create a new project in VB.NET 2008
  2. Display your form in design mode
  3. Add a standard WebBrowser control, dragging it from the toolbox onto the form
  4. Change the WebBrowser's URL property in the propertygrid to something like this: http://www.codeproject.com
  5. Add a Button control from the toolbox onto the form
  6. Change the Text property of the Button from Button1 to Zoom
  7. Switch to the source code edit mode
  8. Replace the code, which should look like this:
Class Form1

End Class

with the following code:

Public Class Form1
Private Enum Exec
  OLECMDID_OPTICAL_ZOOM = 63
End Enum
Private Enum ExecOpt
  OLECMDEXECOPT_DODEFAULT = 0
  OLECMDEXECOPT_PROMPTUSER = 1
  OLECMDEXECOPT_DONTPROMPTUSER = 2
  OLECMDEXECOPT_SHOWHELP = 3
End Enum
Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
  Try
    Dim Res As Object = Nothing
    Dim MyWeb As Object
    MyWeb = Me.WebBrowser1.ActiveXInstance
    MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, _
          ExecOpt.OLECMDEXECOPT_DONTPROMPTUSER, 50, IntPtr.Zero)
  Catch ex As Exception
    MsgBox("Error:" & ex.Message)
  End Try
 End Sub
End Class

Points of interest

The code above only shows the enumeration for zooming a web page. To make use of all of the OLECMDID enumeration commands, simply add them to the Enum Exec shown above. You can find all the available enumerations at MSDN.

History

This is the first release of this solution. If anyone has suggestions on how to make it shorter, let me know.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rocco Labellarte
Chief Technology Officer Vobis Consulting Ltd
United Kingdom United Kingdom
Member
I've been around since the beginning of time (well, nearly fifty years anyway), and I've enjoyed programming since the days of DOS 1.0 with assembly language, MS-BASIC, GW-BASIC, QuickBASIC, Visual BASIC and now VB.NET.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionZoom with Visual Basic. [modified]memberMark AJA2 Oct '11 - 6:52 
To get this to work with Visual Basic,
you need to set the zoom percentage value to a long value with CLng(newValue)
Otherwise you will get the error message:
"Run-time error '-2147221248 (80040100)': Method ExecWB of object 'IWebBrowser2' failed."
 
This is my code:
  On Error Resume Next
  Call WebBrowser1.ExecWB(OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT_DODEFAULT, CLng(newPercentageValue), vbNull)
  
You can get the zoom range with:
  Dim pvaIn As Variant
  Dim pvaOut As Variant
  On Error Resume Next
  pvaIn = vbNull
  pvaOut = vbNull
 
  Call WebBrowser1.ExecWB(OLECMDID_OPTICAL_GETZOOMRANGE, OLECMDEXECOPT_DODEFAULT, pvaIn, pvaOut)
  ' pvaIn = 1
  ' pvaOut = 65536010
Smile | :)

modified 2 Oct '11 - 16:02.

GeneralSelected Tag(s) In WBmembertaqech@Gmail.com20 May '10 - 5:38 
How I Can Get Selected Tags In WebBrowser When selected Any Part Of text
Or how get Selected Blocks?? Cry | :((
GeneralProblems with Web Brower Control in VistamemberFalcon12329 Jul '09 - 18:49 
I not sure if this is the right place to post this question but I have run out of idea's on how to fix this problem, so any assistance you could give me would be a big help!
 
Development System I'm using
Vista Home Edition
Vb.net 2008 Express
 
Problem !!!!
 
I am making a simple web browser using the "Web Browser Control" I can load a web page and navigate until my hearts content. But I can't view Flash content, a screen comes up and say's that I need to download the "Flash Plugin". When I go to the Adobe Flash page and try to get the plugin another page says the "Flash Plugin" is not supported in a 64bit browser. If I use Internet Explorer 7 which was pre installed on the machine when I got it, everything works fine. I have been looking for a work around or a soluion to this problem for months. Any suggestions would be a great help
General'%' of zoommemberIPS Company13 Sep '08 - 9:50 
Does any 1 know how to set the '%' of the zoom?
GeneralRe: '%' of zoommemberJared James Sullivan20 Jan '09 - 15:07 
http://support.microsoft.com/kb/304103[^]
 
Thanks for article!
GeneralError: Trying to revoke a drop target that has not been registered (Exception from HRESULT: 0x80040100 (DRAGDROP_E_NOTREGISTERED))memberTerriTop17 Jun '08 - 9:04 
First off thanks for posting this solution on Code Project. This has plagued me as well, and it is nice to see a solution....well sort of. I implemented the code as described in your post, both with VS2005 and VS2008 and in both cases the following error is thrown: "Trying to revoke a drop target that has not been registered (Exception from HRESULT: 0x80040100 (DRAGDROP_E_NOTREGISTERED))". This obscure error message makes it difficult to resolve the source of the Interop error....Do you have any suggestions for resolving this? Thanks in advance,
GeneralRe: Error: Trying to revoke a drop target that has not been registered (Exception from HRESULT: 0x80040100 (DRAGDROP_E_NOTREGISTERED))memberRocLab17 Jun '08 - 10:57 
I'm running the application on Vista and have Internet Explorer installed. Here is the full source code which definitely works. I assume you are using the Webbrowser component 2.0 which is standard in the toolbox.
 
Public Class Form1
Private Enum Exec
OLECMDID_OPEN = 1
OLECMDID_NEW = 2
OLECMDID_SAVE = 3
OLECMDID_SAVEAS = 4
OLECMDID_SAVECOPYAS = 5
OLECMDID_PRINT = 6
OLECMDID_PRINTPREVIEW = 7
OLECMDID_PAGESETUP = 8
OLECMDID_SPELL = 9
OLECMDID_PROPERTIES = 10
OLECMDID_CUT = 11
OLECMDID_COPY = 12
OLECMDID_PASTE = 13
OLECMDID_PASTESPECIAL = 14
OLECMDID_UNDO = 15
OLECMDID_REDO = 16
OLECMDID_SELECTALL = 17
OLECMDID_CLEARSELECTION = 18
OLECMDID_ZOOM = 19
OLECMDID_GETZOOMRANGE = 20
OLECMDID_UPDATECOMMANDS = 21
OLECMDID_REFRESH = 22
OLECMDID_STOP = 23
OLECMDID_HIDETOOLBARS = 24
OLECMDID_SETPROGRESSMAX = 25
OLECMDID_SETPROGRESSPOS = 26
OLECMDID_SETPROGRESSTEXT = 27
OLECMDID_SETTITLE = 28
OLECMDID_SETDOWNLOADSTATE = 29
OLECMDID_STOPDOWNLOAD = 30
OLECMDID_ONTOOLBARACTIVATED = 31
OLECMDID_FIND = 32
OLECMDID_DELETE = 33
OLECMDID_HTTPEQUIV = 34
OLECMDID_HTTPEQUIV_DONE = 35
OLECMDID_ENABLE_INTERACTION = 36
OLECMDID_ONUNLOAD = 37
OLECMDID_PROPERTYBAG2 = 38
OLECMDID_PREREFRESH = 39
OLECMDID_SHOWSCRIPTERROR = 40
OLECMDID_SHOWMESSAGE = 41
OLECMDID_SHOWFIND = 42
OLECMDID_SHOWPAGESETUP = 43
OLECMDID_SHOWPRINT = 44
OLECMDID_CLOSE = 45
OLECMDID_ALLOWUILESSSAVEAS = 46
OLECMDID_DONTDOWNLOADCSS = 47
OLECMDID_UPDATEPAGESTATUS = 48
OLECMDID_PRINT2 = 49
OLECMDID_PRINTPREVIEW2 = 50
OLECMDID_SETPRINTTEMPLATE = 51
OLECMDID_GETPRINTTEMPLATE = 52
OLECMDID_PAGEACTIONBLOCKED = 55
OLECMDID_PAGEACTIONUIQUERY = 56
OLECMDID_FOCUSVIEWCONTROLS = 57
OLECMDID_FOCUSVIEWCONTROLSQUERY = 58
OLECMDID_SHOWPAGEACTIONMENU = 59
OLECMDID_ADDTRAVELENTRY = 60
OLECMDID_UPDATETRAVELENTRY = 61
OLECMDID_UPDATEBACKFORWARDSTATE = 62
OLECMDID_OPTICAL_ZOOM = 63
OLECMDID_OPTICAL_GETZOOMRANGE = 64
OLECMDID_WINDOWSTATECHANGED = 65
OLECMDID_ACTIVEXINSTALLSCOPE = 66
End Enum
 
Private Enum ExecOpt
OLECMDEXECOPT_DODEFAULT = 0
OLECMDEXECOPT_PROMPTUSER = 1
OLECMDEXECOPT_DONTPROMPTUSER = 2
OLECMDEXECOPT_SHOWHELP = 3
End Enum
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim MyWeb As Object
MyWeb = Me.WebBrowser1.ActiveXInstance
MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, ExecOpt.OLECMDEXECOPT_DONTPROMPTUSER, 50, IntPtr.Zero)
Catch ex As Exception
MsgBox("Error:" & ex.Message)
End Try
End Sub
End Class
GeneralRe: Error: Trying to revoke a drop target that has not been registered (Exception from HRESULT: 0x80040100 (DRAGDROP_E_NOTREGISTERED))memberTerriTop17 Jun '08 - 14:56 
RocLab, thanks for the quick reply. I am running XP2 Pro SP2 with VS2005 and WebBrowser 2.0 as you describe in the article. After doing a little investigation it would appear that this is a side effect of the IE6 browser not implementing the appropriate IWebBrowser2 message constants, which is what my work uses as its standard build. When I implemented this on my home machine (same OS, VS, and WB) but using IE7 the code works fine....Thanks for the help. Looks like one needs to be extra careful, as we will not have control over the machines IE DLL versions and thus may have to disable this feature on pre-IE7 builds. Cheers TerriTop
GeneralRe: Error: Trying to revoke a drop target that has not been registered (Exception from HRESULT: 0x80040100 (DRAGDROP_E_NOTREGISTERED))memberRocLab17 Jun '08 - 11:00 
You may find the solution in this post: http://dn.codegear.com/article/27043
GeneralRe: Error: Trying to revoke a drop target that has not been registered (Exception from HRESULT: 0x80040100 (DRAGDROP_E_NOTREGISTERED))memberRocLab17 Jun '08 - 11:05 
Here are some further suggestions on dealing with the error: http://kbalertz.com/251133/Error-ExecWB-Method.aspx
GeneralRe: Error: Trying to revoke a drop target that has not been registered (Exception from HRESULT: 0x80040100 (DRAGDROP_E_NOTREGISTERED))memberMEIRB11120 May '09 - 12:54 
i have solved the problem very simply if you use variable in the magnifcation size instade of
a number the error is raised so for example:
 
dim fsize as integer
fsize=180
 
'this create error
MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, _
ExecOpt.OLECMDEXECOPT_DONTPROMPTUSER, fsize , IntPtr.Zero)
 
'this one no error simple subtraction FSIZE-0 use this instead
 
MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, _
ExecOpt.OLECMDEXECOPT_DONTPROMPTUSER, fsize - 0, IntPtr.Zero)
GeneralRe: Error: Trying to revoke a drop target that has not been registered (Exception from HRESULT: 0x80040100 (DRAGDROP_E_NOTREGISTERED))memberxZaria24 Jul '09 - 21:49 
The reason the exception is raised in the first place is because the ExecWB third parameter expects an Object, and you are passing it an integer. The argument, fsize - 0, is interpreted as an Object, which is why the code does not produce an error. Therefore, the entire code snippet can be written like this:
Dim fsize As Object = CObj(180)
 
MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, ExecOpt.OLECMDEXECOPT_DONTPROMPTUSER, fsize , Nothing)

 
confused and bemused, maligned and resigned... bewilderment is not a balm for the pain !

GeneralRe: Error: Trying to revoke a drop target that has not been registered (Exception from HRESULT: 0x80040100 (DRAGDROP_E_NOTREGISTERED))memberMinooch4 Aug '10 - 15:45 
Thanks for that.
 
I tried using a trackbar to give the value (percent of zoom) and came to the same problem, after putting trackbar1.value - 0 it work perfectly
 

Thanks
GeneralRe: Error: Trying to revoke a drop target that has not been registered (Exception from HRESULT: 0x80040100 (DRAGDROP_E_NOTREGISTERED))membervinay5718 Jun '11 - 22:03 
Hi , I too have the same problem . I tried the solution provided from u. But it says invalid arguments . I have to provide ref arguments ? What shall i do ? i want so save my webpage from the browser contol to a local file ?
GeneralRe: Error: Trying to revoke a drop target that has not been registered (Exception from HRESULT: 0x80040100 (DRAGDROP_E_NOTREGISTERED))memberfredclown23 Sep '11 - 9:06 
Also if you try this while the IsBusy property is set to true then you will get an error.   So, don't try to set the zoom level on form_load ... instead maybe use the DocumentCompleted event of the webbrowser control.
GeneralRe: Error: Trying to revoke a drop target that has not been registered (Exception from HRESULT: 0x80040100 (DRAGDROP_E_NOTREGISTERED))memberRakesh_Sharma235 Dec '11 - 23:17 
"-0" helped me out ..
 

The code..
 
Private Enum Exec
OLECMDID_OPTICAL_ZOOM = 63
End Enum
Private Enum ExecOpt
OLECMDEXECOPT_DODEFAULT = 0
OLECMDEXECOPT_PROMPTUSER = 1
OLECMDEXECOPT_DONTPROMPTUSER = 2
OLECMDEXECOPT_SHOWHELP = 3
End Enum
 
Private Sub Zoom25Percent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Zoom25Percent.Click
Try
Dim Res As Object = Nothing
Dim MyWeb As Object
MyWeb = Me.wbMain.ActiveXInstance
MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, ExecOpt.OLECMDEXECOPT_DONTPROMPTUSER, 25 - 0, IntPtr.Zero)

Catch
End Try
End Sub

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 17 Jun 2008
Article Copyright 2008 by Rocco Labellarte
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid