设置Word文档的ADRMS权限
引用Microsoft Office 12.0 Object Library
引用Microsoft Word 12.0 Object Library
/// <summary>
/// 设置Word文档权限的工具类
/// </summary>
public class RmsWordHelper : IDisposable
{
/// <summary>
/// Word应用程序对象
/// </summary>
private Microsoft.Office.Interop.Word.ApplicationClass app;
/// <summary>
/// Word文档路径
/// </summary>
private string filePath;
public string FilePath
{
get { return this.filePath; }
set { this.filePath = value; }
}
/// <summary>
/// 权限集合
/// </summary>
private List<RmsWordPermission> rmsPerms = new List<RmsWordPermission>();
public List<RmsWordPermission> RmsPerms
{
get { return this.rmsPerms; }
}
/// <summary>
/// 空值
/// </summary>
private object MISSINGVALUE = System.Reflection.Missing.Value;
/// <summary>
/// 错误消息
/// </summary>
public string ErrMessage = string.Empty;
/// <summary>
/// 是否docx文档
/// </summary>
private bool is2007()
{
bool result = false;
result = System.IO.Path.GetExtension(this.filePath).ToLower() == "docx";
return result;
}
/// <summary>
/// Rms授权后的文件名
/// </summary>
private string GetRmsFileName()
{
string filename = string.Empty;
filename = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(this.filePath), System.IO.Path.GetFileNameWithoutExtension(this.filePath) + "_RMS" + System.IO.Path.GetExtension(this.filePath));
return filename;
}
/// <summary>
/// 保存
/// </summary>
public bool Save()
{
//清除错误消息
this.ErrMessage = string.Empty;
bool result = false;
object filename_src = this.filePath;
object filename_dst = this.GetRmsFileName();
try
{
if (this.app == null)
{
this.app = new Microsoft.Office.Interop.Word.ApplicationClass();
}
var doc = this.app.Documents.Open(ref filename_src, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE,
ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE,
ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE,
ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE);
try
{
doc.Activate();
doc.Permission.Enabled = true;
foreach (var permission in this.rmsPerms)
{
doc.Permission.Add(permission.UserId, permission.Perms, MISSINGVALUE);
}
if (this.is2007())
{
doc.SaveAs(ref filename_dst, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE,
ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE,
ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE,
ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE);
}
else
{
doc.SaveAs2000(ref filename_dst, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE,
ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE,
ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE);
}
result = true;
}
finally
{
doc.Close(ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE);
}
}
catch (Exception ex)
{
this.ErrMessage = ex.Message;
}
return result;
}
/// <summary>
/// 退出Word应用程序
/// </summary>
public void Dispose()
{
if (this.app != null)
{
this.app.Quit(ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE);
this.app = null;
}
}
}
/// 设置Word文档权限的工具类
/// </summary>
public class RmsWordHelper : IDisposable
{
/// <summary>
/// Word应用程序对象
/// </summary>
private Microsoft.Office.Interop.Word.ApplicationClass app;
/// <summary>
/// Word文档路径
/// </summary>
private string filePath;
public string FilePath
{
get { return this.filePath; }
set { this.filePath = value; }
}
/// <summary>
/// 权限集合
/// </summary>
private List<RmsWordPermission> rmsPerms = new List<RmsWordPermission>();
public List<RmsWordPermission> RmsPerms
{
get { return this.rmsPerms; }
}
/// <summary>
/// 空值
/// </summary>
private object MISSINGVALUE = System.Reflection.Missing.Value;
/// <summary>
/// 错误消息
/// </summary>
public string ErrMessage = string.Empty;
/// <summary>
/// 是否docx文档
/// </summary>
private bool is2007()
{
bool result = false;
result = System.IO.Path.GetExtension(this.filePath).ToLower() == "docx";
return result;
}
/// <summary>
/// Rms授权后的文件名
/// </summary>
private string GetRmsFileName()
{
string filename = string.Empty;
filename = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(this.filePath), System.IO.Path.GetFileNameWithoutExtension(this.filePath) + "_RMS" + System.IO.Path.GetExtension(this.filePath));
return filename;
}
/// <summary>
/// 保存
/// </summary>
public bool Save()
{
//清除错误消息
this.ErrMessage = string.Empty;
bool result = false;
object filename_src = this.filePath;
object filename_dst = this.GetRmsFileName();
try
{
if (this.app == null)
{
this.app = new Microsoft.Office.Interop.Word.ApplicationClass();
}
var doc = this.app.Documents.Open(ref filename_src, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE,
ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE,
ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE,
ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE);
try
{
doc.Activate();
doc.Permission.Enabled = true;
foreach (var permission in this.rmsPerms)
{
doc.Permission.Add(permission.UserId, permission.Perms, MISSINGVALUE);
}
if (this.is2007())
{
doc.SaveAs(ref filename_dst, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE,
ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE,
ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE,
ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE);
}
else
{
doc.SaveAs2000(ref filename_dst, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE,
ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE,
ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE);
}
result = true;
}
finally
{
doc.Close(ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE);
}
}
catch (Exception ex)
{
this.ErrMessage = ex.Message;
}
return result;
}
/// <summary>
/// 退出Word应用程序
/// </summary>
public void Dispose()
{
if (this.app != null)
{
this.app.Quit(ref MISSINGVALUE, ref MISSINGVALUE, ref MISSINGVALUE);
this.app = null;
}
}
}
/// <summary>
/// 权限描述
/// </summary>
public class RmsWordPermission
{
/// 权限描述
/// </summary>
public class RmsWordPermission
{
// 域用户邮箱地址,所有用户使用“Everyone”
public string UserId;
public string UserId;
// 权限枚举,多个权限使用|运算符
public Microsoft.Office.Core.MsoPermission Perms;
}
public Microsoft.Office.Core.MsoPermission Perms;
}
使用示例
using (RmsWordHelper helper = new RmsWordHelper())
{
helper.FilePath = filePath;
helper.RmsPerms.Add(new RmsWordPermission() { UserId="Everyone", Perms= Microsoft.Office.Core.MsoPermission.msoPermissionRead });
if (helper.Save())
{
MessageBox.Show("ok");
}
else
{
MessageBox.Show(helper.ErrMessage);
}
}
{
helper.FilePath = filePath;
helper.RmsPerms.Add(new RmsWordPermission() { UserId="Everyone", Perms= Microsoft.Office.Core.MsoPermission.msoPermissionRead });
if (helper.Save())
{
MessageBox.Show("ok");
}
else
{
MessageBox.Show(helper.ErrMessage);
}
}