|
bobby
Posts: 16
Joined: 2012-01-22
|
Hi,
I would like to ins ert a webbrowser object in a slide, and control it using code in my addin.
I found the following topic which adresses exactly this question:
http://www.add-in-express.com/forum/read.php?FID=5&TID=7592
I can create the webBrowser object, but then I did not manage to get control of the webbrowser I created.
Here is my code to create the webbrowser object. It works.
PowerPoint.Shape ShapeWebBrowser = Slide.Shapes.AddOLEObject(
"Shell.Explorer.2", Missing.Value, false, //ClassType, Filename, Link
false, Missing.Value, Missing.Value, Missing.Val ue, //DisplayAsIcon, IconFileName, IconIndex, IconLabel
100, 100, 500, 300); //Left, Top, Width, Height
Then, I have my ShapeWebBrowser object, but I don't know how to use it and access its properties/ functions... Example: how to use the webBrowser.Navigate() function?
I can access the OLE object with ShapeWebBrowser.OLEFormat.Object
The object should have a "Navigate" function, but I cannot use OLEWebBrowser.Navigate("any url").
I tried to cast the object as a "System.Windows.Forms.WebBrowser", but it triggers an error.
System.Windows.Forms.WebBrowser webBrowser1 = (System.Windows.Forms.WebBrowser)ShapeWebBrowser.OLEFormat.Object;
Could you please help me with this issue?
Thanks a lot.
Remark:
I found that the following class may correspond to what I'm looking for : Microsoft.Office.Tools.Excel.Controls.WebBrowser (http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.controls.webbrowser(v=vs.80).aspx)
However, I use Visual Studio Express therefore I think I do not have access to the VSTO library microsoft.office.tools.excel.dll |
|
Posted 23 Sep, 2012 08:11:05
|
|
Top
|
|
Eugene Astafiev
Guest
|
|
Posted 24 Sep, 2012 05:27:08
|
|
Top
|
|
bobby
Posts: 16
Joined: 2012-01-22
|
Thanks for your answer.
Could you please be more specific. I dont know how to use this IWebBrowser2 interface...
- Do I need to add a reference assembly?
- Other method?
I tried with the following code which seems to be similar to what you mentionned: http://www.koders.com/csharp/fid8DF59F8D4E445CA618A0123D21CE2CF10C602289.aspx
I then tried to do: IWebBrowser2 test = (IWebBrowser2)OLEWebBrowser;
But I have an error. Maybe I am not doing it the right way...
Regards, |
|
Posted 24 Sep, 2012 16:04:53
|
|
Top
|
|
Andrei Smolin
Add-in Express team
Posts: 18829
Joined: 2006-05-11
|
Hello Bobby,
bobby writes:
But I have an error.
What error do you get?
I suppose you copied that interface from koders.com tp your code, correct?
Another way to do the same is to use late binding, see System.Type.InvokeMember().
Andrei Smolin
Add-in Express Team Leader |
|
Posted 24 Sep, 2012 23:59:45
|
|
Top
|
|
bobby
Posts: 16
Joined: 2012-01-22
|
OK.
Reminders:
- webBrowserOle is the shape created with WorkSheet.Shapes.AddOLEObject()
- IWebBrowser2 is the interface I copied from koders.com to my code.
1. I get the exception: System.InvalidCastException when I do:
Object OLEWebBrowser = webBrowserOle.OLEFormat.Object;
System.Windows.Forms.WebBrowser test = (System.Windows.Forms.WebBrowser)OLEWebBrowser;
I have the same System.InvalidCastException with:
IWebBrowser2 test = (IWebBrowser2)OLEWebBrowser;
2. I tried using late binding like this (never did it before...)
object[] args = {"http://google.com/"};
Type tp = typeof(System.Windows.Forms.WebBrowser);
tp.InvokeMember("Navigate", BindingFlags.Default | BindingFlags.InvokeMethod, null, OLEWebBrowser, args);
I get the exception: System.Reflection.TargetException: The object does not correspond to target type.
I have the same exception when I define tp as :
Type tp = typeof(IWebBrowser2);
Thanks for your help |
|
Posted 25 Sep, 2012 03:40:28
|
|
Top
|
|
Andrei Smolin
Add-in Express team
Posts: 18829
Joined: 2006-05-11
|
First off, this is doable: see http://support.microsoft.com/kb/291926.
Below is what works for me.
Here's the code creating the control:
webBrowserShape =
PowerPointApp.ActiveWindow.Selection.SlideRange.Shapes.AddOLEObject(
144,
138,
480,
360,
"Shell.Explorer.2",
"",
Microsoft.Office.Core.MsoTriState.msoFalse,
"",
-1,
"",
Microsoft.Office.Core.MsoTriState.msoFalse);
And here's how I handle the SlideShowBegin event:
private void adxPowerPointEvents_SlideShowBegin(object sender, object hostObj)
{
object navigateTo = "www.add-in-express.com";
object flags = 0;
object targetFrameName = "";
object postData = null;
object headers = null;
(webBrowserShape.OLEFormat.Object as IWebBrowser2).Navigate2(ref navigateTo, ref flags, ref targetFrameName, ref postData, ref headers);
}
On IWebBrowser2. I've been unable to compile the koders' version of that interface, so I've taken it and all accompanying enumerations, etc from pinvoke.net, see http://pinvoke.net/default.aspx/Interfaces/IWebBrowser2.html.
Andrei Smolin
Add-in Express Team Leader |
|
Posted 25 Sep, 2012 10:12:35
|
|
Top
|
|
bobby
Posts: 16
Joined: 2012-01-22
|
Hi,
Thank you very much for your support.
I followed your advice. It compiles but I have the following error (in French...):
Une exception de première chance de type 'System.InvalidCastException' s'est produite dans Project.dll
System.InvalidCastException: Impossible d'effectuer un cast d'un objet COM de type 'System.__ComObject' en type d'interface 'Project.Interfaces.IWebBrowser2'.
Cette op?ration a ?chou?, car l'appel QueryInterface sur le composant COM pour l'interface avec l'IID '{D30C1661-CDAF-11D0-8A3E-00C04FC9E26E}' a ?chou? en raison de l'erreur suivante???: Cette interface n?Â?Ð?éest pas prise en charge (Exception de HRESULT : 0x80004002 (E_NOINTERFACE)).
Would it be possible for you to send me the project you managed to compile?
Thanks very much. |
|
Posted 01 Oct, 2012 16:34:53
|
|
Top
|
|
Andrei Smolin
Add-in Express team
Posts: 18829
Joined: 2006-05-11
|
|
Posted 02 Oct, 2012 01:00:34
|
|
Top
|
|