0
我嘗試使用:Microsoft.Office.Interop.Word打開Word文件。 這是我在funcs中的LoadFile代碼:使用Word Interop打開文件Word並替換SharePoint 2010中的一些文本
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中,這個文件是開放的,通過身份識別碼替換[$ ID]。但是我無法在SharePoint 2010中的Web部件中使用此代碼。這是錯誤的。
有人給我一些建議,或者我可以通過另一種方式實現這個目標嗎?
我的目標是:由MS Word打開的文件文件,它取代了一些文本。