c# - 从网页上的按钮打开 Word 应用程序

标签 c# internet-explorer ms-word activex office-interop

我正在开发一个概念验证 Web 应用程序:一个带有按钮的网页,可以打开安装在用户 PC 上的 Word 应用程序。

我在 Visual Studio 2008 Express(Windows XP 客户端、LAMP 服务器)中遇到了一个 C# 项目。我关注了 Writing an ActiveX Control in .NET教程并经过一些调整后它工作正常。然后我添加了用于打开 Word 的按钮。

问题是我可以从项目中引用 Microsoft.Office.Interop.Word,但无法从网页访问它。错误显示“该程序集不允许部分受信任的调用方”。

我已经阅读了很多有关 .NET 安全性的文章,但现在我完全迷失了。免责声明:我从 4 天前开始接触 .NET。

我试图解决这个问题,但我看不到光!! 我什至不知道它是否永远成为可能!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Word = Microsoft.Office.Interop.Word;

using System.IO;
using System.Security.Permissions;

using System.Security;
[assembly: AllowPartiallyTrustedCallers]

namespace OfficeAutomation
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void openWord_Click(object sender, EventArgs e)
        {
            try
            {
                Word.Application Word_App = null;
                Word_App = new Word.Application();
                Word_App.Visible = true;
            }
            catch (Exception exc)
            {
                MessageBox.Show("Can't open Word application (" + exc.ToString() + ")");
            }
        }
    }
}

最佳答案

使用 .Net Framework 4 + XBAP 使这变得简单:您可以使用 WPF XBAP 而不是 ActiveX。

然后在项目设置窗口中执行: 签名:取消选中所有框。 (此项目无需签名), 在“安全”选项卡下,只需将其更改为“完全信任”即可。

如果用户想要允许应用程序运行,将提示用户一次。

关于c# - 从网页上的按钮打开 Word 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2804579/

相关文章:

c# - MVC - 即使没有人登录应用程序也运行计划任务

ms-word - 在 Linux 上以编程方式获取 Microsoft Word 文档的页数

c# - 获取分配给服务器的所有IP

c# - Windows 10 Visual Studio 2019 IIS Express 无法启动并出现错误 500

javascript - Internet Explorer div contentEditable 阻止 DOM 元素选择 onclick

internet-explorer - IE 11 中的 GWT Window.open 误解查询参数 &deg=123

html - Internet Explorer 8 规模问题

browser - word文档查看器浏览器插件

java - 将 MS Word 文档与 Java 合并

c# - 在 Unity 中,如何在外部应用程序中打开文件(即 PDF)?