我尝试使用:Microsoft.Office.Interop.Word打开Word文件。 这是我在funcs LoadFile中的代码:
using Word = Microsoft.Office.Interop.Word;
private void LoadFile( string MyID )
{
Word.Application word = new Word.Application();
Word.Document doc = new Word.Document();
object missing = System.Type.Missing;
try
{
object fileName = "File URL";
doc = word.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);
doc.Activate();
// Replace [$ID] by MyID
foreach (Word.Range tmpRange in doc.StoryRanges)
{
tmpRange.Find.Text = "[$ID]";
tmpRange.Find.Replacement.Text = MyID;
tmpRange.Find.Wrap = Word.WdFindWrap.wdFindContinue;
object replaceAll = Word.WdReplace.wdReplaceAll;
tmpRange.Find.Execute(ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref replaceAll,
ref missing, ref missing, ref missing, ref missing);
}
// And open this file after replace
word.Visible = true;
}
catch (Exception ex)
{
doc.Close(ref missing, ref missing, ref missing);
word.Application.Quit(ref missing, ref missing, ref missing);
}
}
在ASP.NET中,此文件已打开,并由MyID替换[$ ID]。但是我无法在SharePoint 2010中的Webpart中使用此代码。这是错误的。
有人给我一些建议,还是可以通过其他方式实现这个目标?
我的目标是:MS Word打开文件doc并替换了一些文本。
答案 0 :(得分:0)
您的选择包括几个图书馆(免费和商业) - 例如: