我可以使用C#创建和打开Word文档。我可以插入一些新文本,但是现在我试图在此文档上插入曲目更改和注释。
我已经尝试过一些库,但是没有成功。是否有人遇到此类问题或认识一些图书馆(免费或商业)可以帮助我找到该问题的解决方案。
这是用于在新的Word文档中创建和添加文本的代码,在此文档中,我想添加跟踪更改,但是我不知道如何:
public static void CreateDocument()
{
try
{
//Create an instance for word app
Word.Application winword = new Word.Application();
//Set animation status for word application
winword.ShowAnimation = false;
//Set status for word application is to be visible or not.
winword.Visible = false;
//Create a missing variable for missing value
object missing = System.Reflection.Missing.Value;
//Create a new document
Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);
//adding text to document
document.Content.SetRange(0, 0);
document.Content.Text = "In this text I should add track changes " + Environment.NewLine;
//Allow track changes, but I don't know how to use now
document.TrackRevisions = true;
//Save the document
object filename = @"c:\temp1.docx";
document.SaveAs2(ref filename);
document.Close(ref missing, ref missing, ref missing);
document = null;
winword.Quit(ref missing, ref missing, ref missing);
winword = null;
}
catch (Exception ex)
{
}
}
Microsoft当前不建议并且不支持通过任何无人参与的非交互客户端应用程序或组件(包括ASP,ASP.NET,DCOM和NT Services)使Microsoft Office应用程序自动化,因为Office可能表现出不稳定的行为和/在此环境中运行Office时出现死锁或死锁。
如果要构建在服务器端上下文中运行的解决方案,则应尝试使用对无人值守执行安全的组件。或者,您应该尝试找到允许至少部分代码在客户端运行的替代方法。如果您使用服务器端解决方案中的Office应用程序,则该应用程序将缺少许多成功运行所需的功能。此外,您将承担整体解决方案稳定性的风险。在《服务器端Office自动化注意事项》一文中了解有关此内容的更多信息。
作为一种解决方法,如果仅处理开放XML文档,则可以考虑使用Open XML SDK,请参阅欢迎使用Office Open XML SDK 2.5。或者,您可以考虑使用为服务器端执行而设计的第三方组件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句