删除/编辑 C# 中的 word office 内置文档属性

本文关键字:office 内置 文档 属性 word 中的 编辑 删除 | 更新日期: 2023-09-27 18:30:43

请您帮助了解此代码。我正在编写 c# 中的代码,该代码可以清除内置属性中的 Word 文档,并在提供的情况下将其替换为提供的替代品。基于我在microsft支持网站上找到的示例http://support.microsoft.com/kb/303296,我的代码没问题,并且应该可以工作,因为我也没有收到任何编译错误。但没有做我要求它做的事情,因为我没有得到任何结果。请伙计们,如果有人能帮助我解决别有用心的事情或指出我的错误,那么我度过的几周就会白白毁掉,那将不胜感激。谢谢你的帮助。下面是我的代码。

 private void execute_Click(object sender, EventArgs e)
        {
             Word.Application wapp; 
             Word.Document dc = new Word.Document() ;
 Object bSaveChanges = false;
           string chosen_file = "";
           chosen_file = openFD.FileName;
           textBox1.Text = (chosen_file);
           var filter = Path.GetExtension(chosen_file);
           object Filename = chosen_file.ToString();
           if (filter == ".doc" || filter == ".docx")
           {
               wapp = new Word.Application();
               wapp.Visible = true;
               docword = wapp.Documents.Add(ref Filename, ref missing, ref missing, ref missing);
               object _BuiltInProperties = docword.BuiltInDocumentProperties;
                Type typeDocBuiltInProps = _BuiltInProperties.GetType();
                 removeproperty(_BuiltInProperties, typeDocBuiltInProps);// pass parameter
                 docword.Close(ref bSaveChanges, ref missing, ref missing);
                 wapp.Quit(ref bSaveChanges, ref missing, ref missing);
           }
  }
 private void removeproperty(object _BuiltInProperties, Type typeDocBuiltInProps)
        {
            string subjectprop = "Subject";
            string subjectValue = "";
            string companyprop = "Company";
            string companyvalue = txtcompany.Text;
             if (clearsubject.Checked == true)
            {
                try
                {
                    Object Subjectprop = typeDocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties, new object[] { "Subject" });
                    Type typeSubjectprop = Subjectprop.GetType();
                    typeSubjectprop.InvokeMember("Item", BindingFlags.Default | BindingFlags.SetProperty, null, Subjectprop, new object[] { subjectprop, subjectValue  });
                }
                catch (COMException)
                {
                }
            }
       if (resetcompany.Checked == true)
            {
                try
                {
                  Object Companyprop = typeDocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties, new object[] { "Company" });
                    Type typeCompanyprop = Companyprop.GetType();
                    typeCompanyprop.InvokeMember("Item",
                               BindingFlags.Default |
                               BindingFlags.SetProperty,
                               null, Companyprop,
                               new object[] { companyprop, companyvalue });

                }
                 catch (COMException)
                {
                }

}

删除/编辑 C# 中的 word office 内置文档属性

如此处,您可以按名称更改单词属性,如下所示:

void SetWordDocumentPropertyValue(Word.Document document, string propertyName, string propertyValue)
{
  object builtInProperties = document.BuiltInDocumentProperties;
  Type builtInPropertiesType = builtInProperties.GetType();
  object property = builtInPropertiesType.InvokeMember("Item", System.Reflection.BindingFlags.GetProperty, null, builtInProperties, new object[] { propertyName });
  Type propertyType = property.GetType();
  propertyType.InvokeMember("Value", BindingFlags.SetProperty, null, property, new object[] { propertyValue });
  document.UpdateSummaryProperties();
  document.Save();
}

属性名称如"Author""Last Author""Title"等...

1) 添加对 Microsoft.CSharp 的引用。

2) 获取内置文档属性:

private void LoadMetadata(Document Doc) {
  this.Title.Text = Doc.BuiltInDocumentProperties["Title"].Value;
  this.Subject.Text = Doc.BuiltInDocumentProperties["Subject"].Value;
  this.Category.Text = Doc.BuiltInDocumentProperties["Category"].Value;
  this.Keywords.Text = Doc.BuiltInDocumentProperties["Keywords"].Value;
  this.Author.Text = Doc.BuiltInDocumentProperties["Author"].Value;
  this.Comments.Text = Doc.BuiltInDocumentProperties["Comments"].Value;
}

3) 设置新值:

private void SaveMetadata(Document Doc) {
  Doc.BuiltInDocumentProperties["Title"].Value = this.Title.Text;
  Doc.BuiltInDocumentProperties["Subject"].Value = this.Subject.Text;
  Doc.BuiltInDocumentProperties["Category"].Value = this.Category.Text;
  Doc.BuiltInDocumentProperties["Keywords"].Value = this.Keywords.Text;
  Doc.BuiltInDocumentProperties["Author"].Value = this.Author.Text;
  Doc.BuiltInDocumentProperties["Comments"].Value = this.Comments.Text;
}