object oFileName = @"C:/Documents and Settings/liush/My Documents/TestDoc.doc";
object oReadOnly = true;
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;//只是为了方便观察
oDoc = oWord.Documents.Open(ref oFileName, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//MessageBox.Show(oDoc.Tables.Count.ToString());
for (int tablePos = 1; tablePos <= oDoc.Tables.Count; tablePos++)
{
Word.Table nowTable = oDoc.Tables.Item(tablePos);
string tableMessage = string.Format("第{0}/{1}个表:/n", tablePos, oDoc.Tables.Count);
for (int rowPos = 1; rowPos <= nowTable.Rows.Count; rowPos++)
{
for (int columPos = 1; columPos <= nowTable.Columns.Count; columPos++)
{
tableMessage += nowTable.Cell(rowPos, columPos).Range.Text;
tableMessage = tableMessage.Remove(tableMessage.Length - 2, 2);//remove /r/a
tableMessage += "/t";
}
tableMessage += "/n";
}
MessageBox.Show(tableMessage);
}
如果看过了上面kaneboy的文章(这是一个系列的之一),再看这段代码应该不会很难理解。打开一个已有文档,然后遍历其中的所有的表。这里只是简单的将信息显示出来,具体实践上可以对这些信息进行分析。做完这些后,终于找到了一些官方的支持文档,地址如下:
http://msdn2.microsoft.com/zh-CN/library/y1xatbkd.aspx
其中的word任务有对word各种操作的简单代码事例,用vb和c#写的。看完之后,我想每个人都会明白vb对com的支持比c#不是简单明了一点两点。(可以看下这个http://blog.joycode.com/kaneboy/archive/2005/08/03/61489.aspx)同样的代码,用vb实现打开word文档的操作,代码如下:
Dim fileName As String = "C:/Documents and Settings/liush/My Documents/TestDoc.doc"
Dim isReadOnly As Boolean = True
Dim wordApplication As Word.Application = New Word.Application()
Dim wordDocument As Word.Document
wordApplication.Visible = True
wordDocument = wordApplication.Documents.Open(fileName, , isReadOnly)
/***********************************************************************/
在CSDN上总是有网友问这个问题,自己也遇到过,因些写出来供参考:
症状:
oWordApplic = New Word.Application
当程序运行到这句时出现下面的错误:
检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。
oWordApplic = New Word.Application
当程序运行到这句时出现下面的错误:
检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。
解决方法一:
控制面板-》管理工具-》组件服务-》计算机-》我的电脑-》DCom配置-》找到Microsoft Word文档
之后
单击属性打开此应用程序的属性对话框。
2. 单击标识选项卡,然后选择交互式用户。
3.单击"安全"选项卡,分别在"启动和激活权限"和"访问权限"组中选中"自定义",然后
自定义->编辑->添加ASP.NET账户和IUSER_计算机名
* 这些帐户仅在计算机上安装有 IIS 的情况下才存在。
13. 确保允许每个用户访问,然后单击确定。
14. 单击确定关闭 DCOMCNFG。
解决方法二:
如果上述方法不能解决问题,就应该是权限问题,请尝试用下面的方法:
在web.config中使用身份模拟,在<system.web>节中加入 <identity impersonate="true" userName="你的用户名" password="密码"/>
</system.web>
/************************************************************/
protected void Page_Load(object sender, EventArgs e)
{
Word.Application wdApplication; //As Word.Application();
Word.Document wdWords; //As Word.Document
wdApplication = new Word.Application();
Object filename = @"D:/web/image/a.doc";
object oReadOnly = true;
object oMissing = System.Reflection.Missing.Value;
wdWords = wdApplication.Documents.Open(ref filename, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
for (int tablePos = 1; tablePos <= wdWords.Tables.Count; tablePos++)
{
Word.Table nowTable = wdWords.Tables.Item(tablePos);
string tableMessage = string.Format("第{0}/{1}个表:/n", tablePos, wdWords.Tables.Count);
for (int rowPos = 1; rowPos <= nowTable.Rows.Count; rowPos++)
{
for (int columPos = 1; columPos <= nowTable.Columns.Count; columPos++)
{
tableMessage += nowTable.Cell(rowPos, columPos).Range.Text;
tableMessage = tableMessage.Remove(tableMessage.Length - 2, 2);//remove /r/a
tableMessage += "/t";
}
tableMessage += "/n";
}
Response.Write(tableMessage);
}