vba System.Runtime.InteropServices.COMException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6254381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.Runtime.InteropServices.COMException
提问by Maya
I get the following error:
我收到以下错误:
Unable to create instance of class TestProject.TestClass. Error: System.Runtime.InteropServices.COMException: 'D:\Automation\TestProject\OBJECT_DEFINITIONS.XLS' could not be found. Check the spelling of the file name, and verify that the file location is correct. If you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.
无法创建类 TestProject.TestClass 的实例。错误:System.Runtime.InteropServices.COMException:找不到“D:\Automation\TestProject\OBJECT_DEFINITIONS.XLS”。检查文件名的拼写,并验证文件位置是否正确。如果您尝试从最近使用的文件列表中打开该文件,请确保该文件未被重命名、移动或删除。
Error Stack Trace:
错误堆栈跟踪:
Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)
TestProject.TestLibrary.GetObjectDeclarations(String sModule) in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\TestProject\TestLibrary.cs: line 133
TestProject.TestClass..ctor() in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\TestProject\TestClass.cs: line 51
Microsoft.Office.Interop.Excel.Workbooks.Open(字符串文件名、对象更新链接、对象只读、对象格式、对象密码、对象 WriteResPassword、对象 IgnoreReadOnlyRecommended、对象原点、对象分隔符、对象可编辑、对象通知、对象转换器、对象 AddToMru , Object Local, Object CorruptLoad)
TestProject.TestLibrary.GetObjectDeclarations(String sModule) in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\TestProject\TestLibrary.cs: line 133
TestProject.TestClass..ctor( ) 在 C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\TestProject\TestClass.cs 中:第 51 行
code:
代码:
using Excel = Microsoft.Office.Interop.Excel;
namespace TestProject
{
[TestClass]
public class TestLibrary
{
public string[] arrObj = new string[19];
public string[] arrConfig = new string[12];
public string sobjfile;
.
.
.
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(sobjfile, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
I am wondering why is the error saying D:\Automation\TestProject\OBJECT_DEFINITIONS.XLS
could not be found when I have OBJECT_DEFINITIONS.XLS
stored in C:\
我想知道为什么D:\Automation\TestProject\OBJECT_DEFINITIONS.XLS
当我OBJECT_DEFINITIONS.XLS
存储在C:\
回答by evilone
And of course you need to assing path to public string sobjfile
. Application doesn't know where to search your file.
当然,您需要为public string sobjfile
. 应用程序不知道在哪里搜索您的文件。
EDIT:
编辑:
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
MessageBox.Show(xlWorkSheet.get_Range("A1","A1").Value2.ToString());
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
}
回答by rs79
Without delving deep in your code syntax (I'm not a C# programmer), and based on my experience with Selenium, try the following -
无需深入研究您的代码语法(我不是 C# 程序员),根据我使用 Selenium 的经验,请尝试以下操作 -
Ensure that OBJECT_DEFINITIONS.XLS is just that and not OBJECT_DEFINITIONS.XLSX. I have run into Selenium compatibility issues with files created and saved using Excel 2007. If it is, save the excel file as an "Excel 97-2003 Workbook".
确保 OBJECT_DEFINITIONS.XLS 就是这样,而不是 OBJECT_DEFINITIONS.XLSX。我遇到了使用 Excel 2007 创建和保存的文件的 Selenium 兼容性问题。如果是,请将 Excel 文件另存为“Excel 97-2003 工作簿”。