HRESULT:0x80020005 (DISP_E_TYPEMISMATCH)


R

Robert

Hi there,when I try to plug a word into a c# form ,I always get this
exception:HRESULT:0x80020005 (DISP_E_TYPEMISMATCH).
Here is my code:
namespace WordInForm
{
public partial class Form1 : Form
{
Object Nothing = System.Reflection.Missing.Value;
static string strFileName = "";
object pwd = "111";
object OK = true;
AxSHDocVw.AxWebBrowser wb1;
AxSHDocVw.AxWebBrowser wb2;
Microsoft.Office.Interop.Word.Application wordApp;
Microsoft.Office.Interop.Word.Document wordDoc;


public Form1()
{
InitializeComponent();
}

private void NewToolStripMenuItem_Click(object sender, EventArgs e)
{
if (wordDoc != null)
{
this.ReleaseDoc(wordDoc);
}
if (wordApp != null)
{
this.ReleaseApp(wordApp);
}
this.DisposeWebBrowser(wb1);
this.DisposeWebBrowser(wb2);
wb1 = new AxSHDocVw.AxWebBrowser();
wb1.Dock = DockStyle.Fill;
this.Controls.Add(wb1);
object filename = @"c:\1.doc";
Microsoft.Office.Interop.Word.Application App = new
Microsoft.Office.Interop.Word.ApplicationClass();

/***********************************************************************************************************/
/I always get a exception here if create a new doc for the
second time.
Microsoft.Office.Interop.Word.Document Doc =
App.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref OK);
/***********************************************************************************************************/
Doc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref
Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing);
Doc.Close(ref OK, ref Nothing, ref Nothing);
this.ReleaseDoc(Doc);
App.Quit(ref Nothing, ref Nothing, ref Nothing);
this.ReleaseApp(App);
strFileName = filename.ToString();
wb1.NavigateComplete2 += new
AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(wb1_NavigateComplete2);
wb1.Navigate(strFileName);
SaveToolStripMenuItem.Enabled = true;
}

void wb1_NavigateComplete2(object sender,
AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{
wb1.ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS,
SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref Nothing, ref
Nothing);
wb1.Refresh();
Object o = e.pDisp;
Object oDocument = o.GetType().InvokeMember("Document",
BindingFlags.GetProperty, null, o, null);
Object oApplication = o.GetType().InvokeMember("Application",
BindingFlags.GetProperty, null, oDocument, null);
wordApp =
(Microsoft.Office.Interop.Word.Application)oApplication;
wordDoc = (Microsoft.Office.Interop.Word.Document)oDocument;
}

private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
{
this.DisposeWebBrowser(wb1);
this.DisposeWebBrowser(wb2);
wb2 = new AxSHDocVw.AxWebBrowser();
wb2.Dock = DockStyle.Fill;
this.Controls.Add(wb2);
object filename = "";
OpenFileDialog ofp = new OpenFileDialog();
ofp.Multiselect = false;//²»ÔÊÐí¶àÏîÑ¡Ôñ
ofp.InitialDirectory = @"c:\";
ofp.Filter = "doc files(*.doc)|*.doc";
ofp.RestoreDirectory = true;
if (ofp.ShowDialog() == DialogResult.OK)
{
if (ofp.FileName != "")
{
filename = (object)ofp.FileName;
}
}
Microsoft.Office.Interop.Word.Application App = new
Microsoft.Office.Interop.Word.ApplicationClass();

/*******************************************************************************************************/
/Here I can also get a exception if I try to open a doc for the
second time.
Microsoft.Office.Interop.Word.Document Doc =
App.Documents.Open(ref filename, ref Nothing, ref Nothing, ref Nothing, ref
Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref
Nothing);
/*******************************************************************************************************/

if (Doc.ProtectionType !=
Microsoft.Office.Interop.Word.WdProtectionType.wdNoProtection)
{
Doc.Unprotect(ref pwd);
}
Doc.Protect(Microsoft.Office.Interop.Word.WdProtectionType.wdAllowOnlyReading,
ref Nothing, ref pwd, ref Nothing, ref Nothing);

Doc.Close(ref OK, ref Nothing, ref Nothing);
this.ReleaseDoc(Doc);
App.Quit(ref Nothing, ref Nothing, ref Nothing);
this.ReleaseApp(App);
strFileName = filename.ToString();
wb2.NavigateComplete2 += new
AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(wb2_NavigateComplete2);
wb2.Navigate(strFileName);
SaveToolStripMenuItem.Enabled = true;
}

void wb2_NavigateComplete2(object sender,
AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{
wb2.ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS,
SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref Nothing, ref
Nothing);
wb2.Refresh();
Object o = e.pDisp;
Object oDocument = o.GetType().InvokeMember("Document",
BindingFlags.GetProperty, null, o, null);
Object oApplication = o.GetType().InvokeMember("Application",
BindingFlags.GetProperty, null, oDocument, null);
wordApp =
(Microsoft.Office.Interop.Word.Application)oApplication;
wordDoc = (Microsoft.Office.Interop.Word.Document)oDocument;
}

private void ReleaseDoc(Microsoft.Office.Interop.Word.Document
wordDoc)
{
try
{
Marshal.ReleaseComObject(wordDoc);
}
catch (Exception)
{
}
finally
{
wordDoc = null;
}
}

private void ReleaseApp(Microsoft.Office.Interop.Word.Application
wordApp)
{
try
{
Marshal.ReleaseComObject(wordApp);
}
catch (Exception)
{
}
finally
{
wordApp = null;
}
}

private void DisposeWebBrowser(AxSHDocVw.AxWebBrowser wb)
{
if (wb != null)
{
wb.Dispose();
}
}

private void Form1_FormClosing(object sender, FormClosingEventArgs
e)
{
//wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
ReleaseDoc(wordDoc);
//wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
ReleaseApp(wordApp);
try
{
wb1.Dispose();
wb2.Dispose();
}
catch (Exception)
{
}
}

private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
wordDoc.Save();
}
}
}

Somebody help me!!!!!!!
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top