使用Word Interop打开文件Word并替换SharePoint 2010中的某些文本

时间:2012-02-13 08:11:57

标签: sharepoint-2010 interop replace

我尝试使用: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并替换了一些文本。

1 个答案:

答案 0 :(得分:0)

MS在“类似服务器的场景”(IIS / ASP.NET / Sharepoint是一种特殊的场景)中不支持Office Interop - 请参阅http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2

您的选择包括几个图书馆(免费和商业) - 例如: