使用 Word Interop 打开 Word 文件并替换 SharePoint 2010 中的某些文本
我尝试使用: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 中,打开此文件并将 [$ID] 替换为 MyID。但我无法在 SharePoint 2010 的 Web 部件中使用此代码。这是错误的。
有人给我一些建议或者我可以通过其他方式实现这个目标吗?
我的目标是:由 MS Word 打开的文件 doc 并替换了一些文本。
I try to use: Microsoft.Office.Interop.Word to open a Word file.
This is my code in 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);
}
}
In ASP.NET, this file is open and replace [$ID] by MyID. But I can't use this code in a Webpart in SharePoint 2010. It's error.
Do anyone give me some advice or can I achieve this goal by another way?
My goal is: file doc opened by MS Word and it replaced some texts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MS 在“类似服务器的场景”(IIS/ASP.NET/Sharepoint 是一种特殊类型)中不支持 Office Interop - 请参阅 http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2
您的选项包括多个库(免费和商业) - 例如:
Office Interop is NOT supported by MS in "server-like scenarios" (which IIS/ASP.NET/Sharepoint is a special kind of) - see http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2
Your options include several libtraries (free and commercial) - for example: