![](/img/trans.png)
[英]Why is my ICommand.CanExecute(object) parameter null when I set CommandParameter to some Binding, but non-null when I set it to some static value?
[英]Why is my Workbook object null when I set it to activeworkbook?
我曾问过类似的问题,但是我发现没有一个问题可以解决我的具体情况。
我在Visual Studio 2010中有一个用C#编写的Excel 2007 COM加载项。 当我将活动工作簿加载到功能区代码中的工作簿对象中时,它可以正常工作。 当我在Windows窗体中执行此操作时,每次都会从activeworkbook返回null。
以下是我的using语句和错误不断发生的代码段。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using System.Drawing;
---------------------------------------------------
Excel.Application xlApp;
xlApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
Excel.Workbook xlWkbk = (Excel.Workbook)xlApp.ActiveWorkbook;
Excel.Worksheet xlWksht = (Excel.Worksheet)xlWkbk.ActiveSheet;
任何帮助将不胜感激!
该代码本身的工作时间很长,并且仅当您的任务管理器列出了1个EXCEL.EXE进程时才起作用。
我怀疑您正在测试东西并且没有正确处理EXCEL.EXE,或者没有错误处理,因此您编入了错误的EXCEL.EXE进程。
按照此回答添加错误处理程序并正确处理Excel。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.