c# 有关word application和excel application同时使用问题 代码 拿上来看看 应该可以的啊 我就用过Visio和Excel一起编程 代码UP 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 temppath = filepath;//word文档路径 exfilepath = open.FileName;//excel文档路径 object wordpath = temppath ; string expath = exfilepath; //打开word文档 Word.Application newApp = new Word.Application(); deleteword = newApp.NormalTemplate.Path; //控制word关闭事件 Word.ApplicationEvents2_Event wdEvents2 = (Word.ApplicationEvents2_Event)newApp; wdEvents2.Quit += new Word.ApplicationEvents2_QuitEventHandler(QuitHandler); //控制word关闭前事件 wdEvents2 = (Word.ApplicationEvents2_Event)newApp; wdEvents2.DocumentBeforeClose += new Word.ApplicationEvents2_DocumentBeforeCloseEventHandler(myWordApp_DocumentBeforeClose); // 打开文档函数 doc = newApp.Documents.Open(ref wordpath, ref Unknown, ref readOnly, ref Unknown, ref Unknown,ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,ref Unknown, ref Unknown, ref Unknown,ref Unknown, ref Unknown); foreach (Office.CommandBar cb in doc.CommandBars) { foreach (Office.CommandBarControl cbc in cb.Controls) { if (cb.Name == "Standard" && (cbc.Caption == "保存(&S)" || cbc.Caption == "打开(&O)..." || cbc.Caption == "新建空白文档(&B)")) { cbc.Enabled = false; cbc.Visible = false; }if (cb.Name == "Menu Bar" && ((cbc.Caption == "文件(&F)") || (cbc.Caption == "工具(&T)"))) { cbc.Enabled = false; cbc.Visible = false; } } } newApp.Visible = true;//显示word文档 newApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;//禁用word原有弹出对话框 object missing = System.Reflection.Missing.Value; //创建自定义菜单条 Office.CommandBarPopup cmdBarControl1 = null; Office.CommandBar commandBar = (Office.CommandBar)newApp.CommandBars.ActiveMenuBar; int controlCount1 = commandBar.Controls.Count; //添加文件菜单条 string menuCaption = "文件(&P)"; cmdBarControl1 = (Office.CommandBarPopup)commandBar.Controls.Add(Office.MsoControlType.msoControlPopup, missing, missing, 2, true); cmdBarControl1.Tag = "文件"; cmdBarControl1.Caption = menuCaption; commandBar.Visible = true; }Office.CommandBarButton wordbtn2 = cmdBarControl1.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true) as Office.CommandBarButton; wordbtn2.Caption = "新建"; wordbtn2.Tag = "新建"; wordbtn2.Enabled = true; wordbtn2.Style = Office.MsoButtonStyle.msoButtonCaption; wordbtn2.FaceId = 0162; wordbtn2.Click += new Office._CommandBarButtonEvents_ClickEventHandler(wordbtn2_Click); //excel文档 newexAPP = new Excel.Application(); //excel关闭前事件 Excel.AppEvents_WorkbookBeforeCloseEventHandler EventDel_BeforeBookClose; EventDel_BeforeBookClose = new Excel.AppEvents_WorkbookBeforeCloseEventHandler(BeforeBookClose); newexAPP.WorkbookBeforeClose += EventDel_BeforeBookClose; // 打开excel文档函数 xls = newexAPP.Workbooks.Open(expath, readOnly,Unknown,Unknown, Unknown,Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown,Unknown); //禁用excel文档某些原有菜单按钮 foreach (Office.CommandBar cb in newexAPP.CommandBars) { foreach (Office.CommandBarControl cbc in cb.Controls) { if (cb.Name == "Standard" && (cbc.Caption == "保存(&S)" || cbc.Caption == "打开" || cbc.Caption == "新建(&N)")) { cbc.Enabled = false; cbc.Visible = false; } if (cb.Name == "Worksheet Menu Bar" && ((cbc.Caption == "文件(&F)") || (cbc.Caption == "工具(&T)"))) { cbc.Enabled = false; cbc.Visible = false; } } } newexAPP.DisplayStatusBar = false; newexAPP.Visible = true; object missing = System.Reflection.Missing.Value; //创建文件菜单条 Office.CommandBarPopup cmdBarControl = null; Office.CommandBar excommandBar = (Office.CommandBar)newexAPP.CommandBars.ActiveMenuBar; int controlCount = excommandBar.Controls.Count; string menuCaption = "文件(&P)"; cmdBarControl = (Office.CommandBarPopup)excommandBar.Controls.Add(Office.MsoControlType.msoControlPopup, missing, missing, 2, true); cmdBarControl.Tag = "文件"; if (cmdBarControl != null) { cmdBarControl.Caption = menuCaption; //添加excel新建文档按钮 Office.CommandBarButton excelbtn4 = cmdBarControl.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true) as Office.CommandBarButton; excelbtn4.Caption = "新建"; excelbtn4.Tag = "新建"; excelbtn4.Enabled = true; excelbtn4.Style = Office.MsoButtonStyle.msoButtonCaption; excelbtn4.FaceId = 0162; excelbtn4.Click += new Office._CommandBarButtonEvents_ClickEventHandler(excelbtn4_Click); 不是很清楚,要操作某文档,是否应该先active这个。 是啊,如果是涉及的只是word文档的话,可以doc.active,可是excel文档一打开,这个doc.active好像不起作用了 优化InputAttributes.Add textbox的内容总是残留 C#制作文件安装程序 大侠们,在C#中如何定义一个无限大的数组? 关于类似FTP下载的源码? 在vs中如何用C#画出一个三角形? 如何在服务器端调用客户端函数? C#从C读取utf-8字符串,win8下正常,xp下读出来为null 怎样把STRUCTURE类型的数据转化成BYTE数组类型 关于配置的问题,在web服务器上无法调试,在线等 System.IO.Directory.GetCurrentDirectory() C#開發的Windows服務程序 編繹OK,在安裝後啟動出錯
temppath = filepath;//word文档路径
exfilepath = open.FileName;//excel文档路径
object wordpath = temppath ;
string expath = exfilepath;
//打开word文档
Word.Application newApp = new Word.Application();
deleteword = newApp.NormalTemplate.Path;
//控制word关闭事件
Word.ApplicationEvents2_Event wdEvents2 = (Word.ApplicationEvents2_Event)newApp;
wdEvents2.Quit += new Word.ApplicationEvents2_QuitEventHandler(QuitHandler); //控制word关闭前事件
wdEvents2 = (Word.ApplicationEvents2_Event)newApp;
wdEvents2.DocumentBeforeClose += new Word.ApplicationEvents2_DocumentBeforeCloseEventHandler(myWordApp_DocumentBeforeClose);
// 打开文档函数
doc = newApp.Documents.Open(ref wordpath, ref Unknown, ref readOnly, ref Unknown, ref Unknown,ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,ref Unknown, ref Unknown, ref Unknown,ref Unknown, ref Unknown);
foreach (Office.CommandBar cb in doc.CommandBars)
{
foreach (Office.CommandBarControl cbc in cb.Controls)
{
if (cb.Name == "Standard" && (cbc.Caption == "保存(&S)" || cbc.Caption == "打开(&O)..." || cbc.Caption == "新建空白文档(&B)"))
{
cbc.Enabled = false;
cbc.Visible = false; }
if (cb.Name == "Menu Bar" && ((cbc.Caption == "文件(&F)") || (cbc.Caption == "工具(&T)")))
{
cbc.Enabled = false;
cbc.Visible = false; } } }
newApp.Visible = true;//显示word文档
newApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;//禁用word原有弹出对话框
object missing = System.Reflection.Missing.Value; //创建自定义菜单条
Office.CommandBarPopup cmdBarControl1 = null;
Office.CommandBar commandBar = (Office.CommandBar)newApp.CommandBars.ActiveMenuBar;
int controlCount1 = commandBar.Controls.Count;
//添加文件菜单条
string menuCaption = "文件(&P)";
cmdBarControl1 = (Office.CommandBarPopup)commandBar.Controls.Add(Office.MsoControlType.msoControlPopup, missing, missing, 2, true);
cmdBarControl1.Tag = "文件";
cmdBarControl1.Caption = menuCaption;
commandBar.Visible = true;
}
Office.CommandBarButton wordbtn2 = cmdBarControl1.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true) as Office.CommandBarButton; wordbtn2.Caption = "新建";
wordbtn2.Tag = "新建";
wordbtn2.Enabled = true;
wordbtn2.Style = Office.MsoButtonStyle.msoButtonCaption;
wordbtn2.FaceId = 0162;
wordbtn2.Click += new Office._CommandBarButtonEvents_ClickEventHandler(wordbtn2_Click); //excel文档
newexAPP = new Excel.Application();
//excel关闭前事件
Excel.AppEvents_WorkbookBeforeCloseEventHandler EventDel_BeforeBookClose;
EventDel_BeforeBookClose = new Excel.AppEvents_WorkbookBeforeCloseEventHandler(BeforeBookClose);
newexAPP.WorkbookBeforeClose += EventDel_BeforeBookClose; // 打开excel文档函数
xls = newexAPP.Workbooks.Open(expath, readOnly,Unknown,Unknown, Unknown,Unknown, Unknown, Unknown,
Unknown, Unknown, Unknown, Unknown, Unknown, Unknown,Unknown); //禁用excel文档某些原有菜单按钮
foreach (Office.CommandBar cb in newexAPP.CommandBars)
{
foreach (Office.CommandBarControl cbc in cb.Controls)
{
if (cb.Name == "Standard" && (cbc.Caption == "保存(&S)" || cbc.Caption == "打开" || cbc.Caption == "新建(&N)"))
{
cbc.Enabled = false;
cbc.Visible = false; } if (cb.Name == "Worksheet Menu Bar" && ((cbc.Caption == "文件(&F)") || (cbc.Caption == "工具(&T)")))
{
cbc.Enabled = false;
cbc.Visible = false;
}
}
}
newexAPP.DisplayStatusBar = false;
newexAPP.Visible = true;
object missing = System.Reflection.Missing.Value; //创建文件菜单条
Office.CommandBarPopup cmdBarControl = null;
Office.CommandBar excommandBar = (Office.CommandBar)newexAPP.CommandBars.ActiveMenuBar;
int controlCount = excommandBar.Controls.Count;
string menuCaption = "文件(&P)";
cmdBarControl = (Office.CommandBarPopup)excommandBar.Controls.Add(Office.MsoControlType.msoControlPopup, missing, missing, 2, true);
cmdBarControl.Tag = "文件"; if (cmdBarControl != null)
{
cmdBarControl.Caption = menuCaption;
//添加excel新建文档按钮
Office.CommandBarButton excelbtn4 = cmdBarControl.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true) as Office.CommandBarButton; excelbtn4.Caption = "新建";
excelbtn4.Tag = "新建";
excelbtn4.Enabled = true;
excelbtn4.Style = Office.MsoButtonStyle.msoButtonCaption;
excelbtn4.FaceId = 0162;
excelbtn4.Click += new Office._CommandBarButtonEvents_ClickEventHandler(excelbtn4_Click);
不是很清楚,要操作某文档,是否应该先active这个。