c#操作word文件

aimyray 2009-06-16 03:01:00
添加引用:using Microsoft.Office.Interop.Word;

打开一个已经存在的word文档,
/// <summary>
/// 打开Word文档,
/// </summary>
/// <param name="openFileName">完整的文件名</param>
/// <param name="onlyRead">指定文件是否只读</param>
/// <param name="docIsVisible">确定新建的word文档是否可视</param>
public static void OpenWordFile(string openFileName, bool onlyRead, bool docIsVisible)
{
try
{
if (appWord == null)
{
appWord = new ApplicationClass();
}

if (File.Exists(openFileName))
{
appWord.Visible = true;
WordVisible = true;

object visible = true;
object fileName = openFileName;
object confirmConversions = false;//允许转换
object readOnly = onlyRead;//只读
object addToRecentFiles = false;//添加到最近打开的文档
object passWordDocument = System.Reflection.Missing.Value;
object passWordTemplate = System.Reflection.Missing.Value;
object revert = System.Reflection.Missing.Value;
object writePasswordDocument = System.Reflection.Missing.Value;
object writePasswordTemplate = System.Reflection.Missing.Value;
object format = System.Reflection.Missing.Value;
object encoding = System.Reflection.Missing.Value;
object Visible = docIsVisible;
object openAndRepair = System.Reflection.Missing.Value;
object documentDirection = System.Reflection.Missing.Value;
object noEncodingDialog = System.Reflection.Missing.Value;
object xmlTransform = System.Reflection.Missing.Value;

activeDoc = appWord.Documents.Open(ref fileName, ref confirmConversions, ref readOnly, ref addToRecentFiles, ref passWordDocument,
ref passWordTemplate, ref revert, ref writePasswordDocument, ref writePasswordTemplate, ref format, ref encoding,
ref Visible, ref openAndRepair, ref documentDirection, ref noEncodingDialog, ref xmlTransform);
activeDoc.Activate();
}
else
{
NewWordFile(openFileName, docIsVisible, true);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 新建Word文档
/// </summary>
/// <param name="newFileName">完整的文件名</param>
/// <param name="docIsVisible">确定新建的word文档是否可视</param>
/// <param name="wordIsVisible">确定word应用程序是否可视</param>
public static void NewWordFile(string newFileName, bool docIsVisible, bool wordIsVisible)
{
try
{
if (appWord == null)
{
appWord = new ApplicationClass();
}

appWord.Visible = wordIsVisible;
WordVisible = wordIsVisible;

if (File.Exists(newFileName))
{//判断文件是否存在,若存在,则删除
File.Delete(newFileName);
}

object Nothing = System.Reflection.Missing.Value;
object documentType = Microsoft.Office.Interop.Word.WdNewDocumentType.wdNewBlankDocument;
object visible = docIsVisible;

activeDoc = appWord.Documents.Add(ref Nothing, ref Nothing, ref documentType, ref visible);

object fileName = newFileName;
object fileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
object lockComments = false;

object passWord = System.Reflection.Missing.Value;
object addToRecentFiles = true;
object writePassWord = System.Reflection.Missing.Value;
object readOnlyRecommended = false;
object embedTrueTypeFonts = false;
object saveNativePictiureFormat = false;
object saveFormsData = false;
object saveAsAOCELetter = false;
object unknown = System.Reflection.Missing.Value;

//保存文档
activeDoc.SaveAs(ref fileName, ref fileFormat, ref lockComments, ref passWord, ref addToRecentFiles, ref writePassWord,
ref readOnlyRecommended, ref embedTrueTypeFonts, ref saveNativePictiureFormat, ref saveFormsData, ref saveAsAOCELetter,
ref unknown, ref unknown, ref unknown, ref unknown, ref unknown);
activeDoc.Activate();

}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}


}
...全文
497 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
aimyray 2009-06-17
  • 打赏
  • 举报
回复
我在要打开一个word文档的代码前加了这样的代码:
activeDoc = null;
appWord = null;
GC.Collect();
System.Diagnostics.Process[] myProcesses;
myProcesses = System.Diagnostics.Process.GetProcessesByName("WINWORD");

foreach (System.Diagnostics.Process instance in myProcesses)
{
instance.Kill();
}

这样打开一个文档后,直接在word中关闭,再重新打开,就可以了!不知道我的这种方式可正确?会不会过早地调用了GC.Collect()对系统的其他部分产生影响?
wuyq11 2009-06-16
  • 打赏
  • 举报
回复
RPC服务器不可以用与IIS配置有关关闭word
activeDoc .Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
appWord.Quit(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(Doc);
activeDoc =null;
appWord =null;
GC.Collect();
GC.Collect();
System.Diagnostics.Process[] myProcesses;
myProcesses =
System.Diagnostics.Process.GetProcessesByName("WINWORD");
foreach (System.Diagnostics.Process instance in myProcesses)
{
instance.Kill();
}
deng520159 2009-06-16
  • 打赏
  • 举报
回复
wordapplication 要关闭哦,
public void Dispose()
{
try
{
if (m_WordApp != null)
m_WordApp.Quit(ref missing, ref missing, ref missing);
}
catch (Exception ex)
{
Debug.Write(ex.ToString());
}
}
#endregion
}
seraphgxh 2009-06-16
  • 打赏
  • 举报
回复
ding, studying...
aimyray 2009-06-16
  • 打赏
  • 举报
回复
文件释放?能具体点么?我已经把word都给关了啊!最好能有代码提示下,谢谢!
wade_li 2009-06-16
  • 打赏
  • 举报
回复
似乎是文件打开后没有释放!
wewei123shangdawei 2009-06-16
  • 打赏
  • 举报
回复
我也遇到此问题,但没有解决
aimyray 2009-06-16
  • 打赏
  • 举报
回复
当打开一个文档,关闭后,重新再打开的时候弹出异常信息:RPC服务器不可以用。(异常来自于HRESULT:0x900706BA)。是怎么回事啊?

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧