2010-07-27 65 views
2

我正在處理文檔管理程序。我想通過點擊一個按鈕打開一個Word文檔,並根據用戶的權限給予登錄的用戶一定的權限(如只讀,編輯表)。通過C#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; 

此代碼是開放的文檔,但我已經設置了只讀真實的,它應該以只讀模式打開文檔。當文檔在Word 2003中打開時,它是可編輯的,而在Word 2010中打開時,它是隻讀的。

此外,我想知道是否值得它禁用Word的所有工具欄,並創建我自己的按鈕,我想要的。我只想給我想要的功能 - 比如我想禁用SaveAs功能。

回答

0

請看下面的代碼。 通過指定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)); 
      } 
     } 

    } 
}