我在设置的 ASP.NET 网站上遇到了一些问题,而且很难调试。
背景资料:
我的网站上有一个页面允许用户上传一个或多个 Microsoft Word 文档。然后用户可以按下一个按钮,代码应该打开文档,计算单词,然后返回表格中的单词数。
当我在 Visual Studio 中运行调试器时,这工作得非常好,但是当我尝试从另一台计算机通过 Web 执行此操作时,我得到一个错误。
这是一些代码。我试图尽可能地简化它。
// List of int's to hold the number of words in each document
List<int> words = new List<int>();
// Loop through the files that the user selected
// (The files have already been uploaded, and now their path is in "lstFileBox")
for (int i = 0; i < this.lstFileBox.Items.Count; i++)
{
try
{
String file = this.lstFileBox.Items[i].Text;
// MicrosoftWordOperations is a custom class
MicrosoftWordOperations wordOps = new MicrosoftWordOperations(file);
String contents = wordOps.GetContents();
int numWords = wordOps.CountWords(contents);
// Add number of words to my list
words.Add(numWords);
// Delete the uploaded file, which was stored in a temporary location
if (System.IO.File.Exists(file))
System.IO.File.Delete(file);
}
catch (Exception e)
{
}
}
// ...
// Then add number of words to a table
// ...
MicrosoftWordOperations
代码非常基本:
public class MicrosoftWordOperations
{
private String _file;
public MicrosoftWordOperations(String file)
{
this._file = file;
}
public String GetContents()
{
object fileName = (object)this._file;
object missing = System.Reflection.Missing.Value;
Word.Application wordObject = new Word.Application();
Word.Document wordDocument = wordObject.Documents.Open(
ref fileName, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
Word.Document activeDocument = wordObject.ActiveDocument;
String fileContents = activeDocument.Content.Text;
wordDocument.Close(ref missing, ref missing, ref missing);
return fileContents;
}
public int CountWords(String text)
{
MatchCollection collection = Regex.Matches(text, @"[\S]+");
return collection.Count;
}
}
编辑:
我能够进行一些基本的调试,这是在第一个代码块中捕获的异常:
System.UnauthorizedAccessException:由于以下错误,检索具有 CLSID {000209FF-0000-0000-C000-000000000046} 的组件的 COM 类工厂失败:80070005 访问被拒绝。(来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))。在 [path]\MicrosoftWordOperations.cs:line 26 中的 MicrosoftWordOperations.GetContents() [path]\WordCounter.aspx.cs:line 69 中的 Content_WordCounter.CountWords()
编辑:
MSWord 安装在服务器上。
编辑:
第 26 行:Word.Application wordObject = new Word.Application();
第 69 行:String contents = wordOps.GetContents();