marvin pereda

marvin pereda

  • NA
  • 2
  • 3.1k

Embedding MS Office on a Form

Sep 6 2012 11:23 AM
I am trying http://support.microsoft.com/kb/304662 but I am having an error
Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME)) 

in this line
[code] Object oApplication = o.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, oDocument, null); [/code]

Here's the code

[code]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace GoPPT2
{
public partial class Form1 : Form
{
private Object oDocument;
public Form1()
{
InitializeComponent();
this.axWebBrowser1.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.axWebBrowser1_NavigateComplete2);
this.Load += new System.EventHandler(this.Form1_Load);
this.Closed += new System.EventHandler(this.Form1_Closed);
}

private void button1_Click(object sender, System.EventArgs e)
{

String strFileName;

//Find the Office document.
openFileDialog1.FileName = "";
openFileDialog1.ShowDialog();
strFileName = openFileDialog1.FileName;

//If the user does not cancel, open the document.
if (strFileName.Length != 0)
{
Object refmissing = System.Reflection.Missing.Value;
oDocument = null;
axWebBrowser1.Navigate(strFileName, ref refmissing, ref refmissing, ref refmissing, ref refmissing);
}
}

public void Form1_Load(object sender, System.EventArgs e)
{
button1.Text = "Browse";
openFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.pptx)|*.doc;*.xls;*.pptx";
openFileDialog1.FilterIndex = 1;
}

public void Form1_Closed(object sender, System.EventArgs e)
{
oDocument = null;
}

private void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{
Object o = e.pDisp;

oDocument = o.GetType().InvokeMember("Document", BindingFlags.GetProperty, null, o, null);

Object oApplication = o.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, oDocument, null);

Object oName = o.GetType().InvokeMember("Name", BindingFlags.GetProperty, null, oApplication, null);

MessageBox.Show("File opened by: " + oName.ToString());
}

}
}
[/code]
PS : I have Microsoft Office 2010 installed and I am using Visual Studio 2010 for this.
Hope you could help me!

Answers (2)