通过C#实现Word文档权限

发布于 2024-09-11 12:48:44 字数 765 浏览 0 评论 0原文

我正在开发一个文档管理程序。我想通过单击按钮打开一个Word文档,并且根据登录用户的权限将被授予一定的权限(例如只读,可编辑)。

我尝试使用以下代码:

object missing = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word.ApplicationClass doc = new Microsoft.Office.Interop.Word.ApplicationClass();       

object name = @"some.doc";
object read = true;
object t = true;
doc.Documents.Open(ref name, ref missing, ref read, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref t, ref missing, ref missing, ref missing, ref missing);

doc.Visible = true;

此代码正在打开文档,但由于我已将只读设置为 true,因此它应该以只读模式打开文档。当文档在 Word 2003 中打开时,它是可编辑的,而在 Word 2010 中打开时,它是只读的。

另外,我想知道是否值得禁用Word的所有工具栏并使用我想要的按钮创建自己的工具栏。我只想提供我想要的功能 - 就像我想禁用“另存为”功能一样。

I am working on a document management program. I want to open up a Word document with the click of a button and according to the permissions to the user whoever logged in will be given certain permissions (like read-only, edittable).

I tried with this code:

object missing = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word.ApplicationClass doc = new Microsoft.Office.Interop.Word.ApplicationClass();       

object name = @"some.doc";
object read = true;
object t = true;
doc.Documents.Open(ref name, ref missing, ref read, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref t, ref missing, ref missing, ref missing, ref missing);

doc.Visible = true;

This code is opening up the document but as I have set the read-only true, it should open the document in readonly mode. When document is opened in Word 2003 it is editable whereas when opened in Word 2010 it is read-only.

Also, I'd like to know if is it worth it to disable all the toolbars of Word and create my own with the buttons which I want. I want to give only the functionality which I want - like I want to disable the SaveAs functionality.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

羁绊已千年 2024-09-18 12:48:44

像下面的代码怎么样?
通过指定 proc.StartInfo.UseShellExecute = true,Windows 将为当前用户自动启动与 Word 文档关联的应用程序。

namespace ConsoleApplication1
{
    using System.Diagnostics;
    using System.Windows.Forms;

    class Program
    {
        static void Main( string[] args )
        {
            var docName = @"some.doc";

            try
            {
                using ( var proc = new Process() )
                {
                    proc.StartInfo.UseShellExecute = true;

                    proc.StartInfo.FileName = docName;

                    proc.Start();
                }
            }
            catch ( System.Exception ex )
            {
                MessageBox.Show( string.Format( "Unable to display document '{0}': {1}", docName, ex.Message ) );
            }
        }

    }
}

How about something like the following code.
By specifying proc.StartInfo.UseShellExecute = true, Windows will automatically launch the application associated with the Word document, for the current user.

namespace ConsoleApplication1
{
    using System.Diagnostics;
    using System.Windows.Forms;

    class Program
    {
        static void Main( string[] args )
        {
            var docName = @"some.doc";

            try
            {
                using ( var proc = new Process() )
                {
                    proc.StartInfo.UseShellExecute = true;

                    proc.StartInfo.FileName = docName;

                    proc.Start();
                }
            }
            catch ( System.Exception ex )
            {
                MessageBox.Show( string.Format( "Unable to display document '{0}': {1}", docName, ex.Message ) );
            }
        }

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