学习的日记,以后可能有用,先记录下来:在c#中嵌入打开office文件。在office2003中不会弹出打开提示,但据说在office2007中会弹出打开提示,不知道真的假的,有待确认(down个office2007麻烦啊!)。
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;
using Microsoft.Office.Core;
//using Word;
//using AxShDocVw;
using SHDocVw;
using SonTaBll;
namespace SonTa
{
public partial class frmClass : Form
{
public frmClass()
{
InitializeComponent();
}
private Object oDocument;
public int type = -1;
String strFileName = "";
private void frmFirstClass_Load(object sender, EventArgs e)
{
OpenFile();
}
public void OpenFile()
{
OpenFileDialog ofDialog = new OpenFileDialog();
ofDialog.AddExtension = true;
ofDialog.CheckFileExists = true;
ofDialog.CheckPathExists = true;
ofDialog.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt";
ofDialog.DefaultExt = "*.doc";
if (ofDialog.ShowDialog() == DialogResult.OK)
{
strFileName = ofDialog.FileName;
}
if (strFileName.Length != 0)
{
Object refmissing = System.Reflection.Missing.Value;
oDocument = null;
axWebBrowser.Navigate(strFileName, ref refmissing, ref refmissing, ref refmissing, ref refmissing);
}
}
private void frmFirstClass_FormClosed(object sender, FormClosedEventArgs e)
{
this.axWebBrowser.Dispose();
oDocument = null;
}
private void axWebBrowser_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{
//Note: You can use the reference to the document object to
// automate the document server.
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());
}