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

发布于 2025-01-05 03:20:05 字数 1893 浏览 1 评论 0原文

我尝试使用: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从来不烧饼 2025-01-12 03:20:05

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

您的选项包括多个库(免费和商业) - 例如:

  • < a href="http://www.microsoft.com/download/en/details.aspx?id=5124" rel="nofollow">MS OpenXML SDK(免费)
  • Aspose.Words(商业)

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文