在c#中删除/编辑word office builtindocumentproperties [英] Remove/Edit word office builtindocumentproperties in c#
问题描述
关于此代码,请求您的帮助。我正在编写一个c#中的代码,它可以清除属性中内置的word文档,并在提供的替换时将其替换为提供的替换。基于我在microsft支持网站http://support.microsoft.com/kb/303296上在网上找到的例子,我的代码没问题,并且假设工作,因为我也没有得到任何编译错误。但是没有做我要求它做的事情,因为我没有得到任何结果。请大家,如果有人会帮助我替代或者指出我的错误,那么我真的很感激,这样我花的几周就会白白浪费。谢谢你的帮助。以下是我的代码。
private void execute_Click( object sender,EventArgs e)
{
Word。申请表;
Word.Document dc = new Word.Document();
对象 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 文件名,参考缺失, ref 缺失, ref 缺失);
object _BuiltInProperties = docword.BuiltInDocumentProperties;
类型typeDocBuiltInProps = _BuiltInProperties.GetType();
removeproperty(_BuiltInProperties,typeDocBuiltInProps); // 传递参数
docword.Close( ref bSaveChanges, ref 缺失, ref 缺少);
wapp.Quit( ref bSaveChanges, ref 缺失,参考缺失);
}
}
私有 void removeproperty( object _BuiltInProperties,Type typeDocBuiltInProps)
{
string subjectprop = 主题;
string subjectValue = ;
string companyprop = 公司跨度>;
string companyvalue = txtcompany.Text;
if (clearsubject.Checked == true )
{
尝试
{
对象 Subjectprop = typeDocBuiltInProps.InvokeMember(< span class =code-string> Item,BindingFlags.Default | BindingFlags.GetProperty, null ,_ BuildInProperties, new object [] { 主题});
类型typeSubjectprop = Subjectprop.GetType();
typeSubjectprop.InvokeMember( Item,BindingFlags.Default | BindingFlags。 SetProperty, null ,Subjectprop, new object [] {subjectprop,subjectValue});
}
catch (COMException)
{
}
}
if (resetcompany.Checked == true )
{
尝试
{
对象 Companyprop = typeDocBuiltInProps .InvokeMember( Item,BindingFlags.Default | BindingFlags.GetProperty, null ,_ BuildInProperties, new object [] { 公司});
类型typeCompanyprop = Companyprop.GetType();
typeCompanyprop.InvokeMember( Item,
BindingFlags.Default |
BindingFlags.SetProperty,
null ,Companyprop,
new object [] {companyprop,companyvalue});
}
catch (COMException)
{
}
}
对于 C#DOCX 文件格式,您可以使用此C# / VB.NET Word 库。
这是一个示例C#代码:
// 在免费模式下使用该组件。
ComponentInfo.SetLicense( FREE-LIMITED-KEY);
// 创建新的空文档。
var document = new DocumentModel();
// 添加各种内置文档属性。
document.DocumentProperties.BuiltIn.Add(BuiltInDocumentProperty.Title, 我的文档标题);
document.DocumentProperties.BuiltIn.Add(BuiltInDocumentProperty.Subject, 我的文档主题) ;
document.DocumentProperties.BuiltIn.Add(BuiltInDocumentProperty.Author, John Doe);
document.DocumentProperties.BuiltIn.Add(BuiltInDocumentProperty.Keywords, keyword1,keyword2,keyword3);
// 将文档保存到文件中。
document .Save( Document.docx,SaveOptions.DocxDefault);
// 从文件加载文档。
文档= DocumentModel.Load( Document.docx,LoadOptions.DocxDefault);
// 迭代内置文档属性并将其写入Console。
foreach ( var builtInProperty in document.DocumentProperties.BuiltIn)
Console.WriteLine( {0}:{1},builtInProperty.Key,builtInProperty.Value);
我不知道你现在是否需要解决方案但是我做了这件事。 br />
我的代码在你的版本中也没有用。
我删除外部方法removeproperty并将所有代码放到execute_Click并且情况变好了。
Please need your help regarding this code. I am writing a code in c# that can will clear a word document built in property, and also replace it with a provided replacement where provided. Base on the example i found online in microsft support website http://support.microsoft.com/kb/303296 , my code is alright and suppose to work as i don''t get any compile error as well. but is not doing what i ask it to do as i don''t get any result. Please guys , will really appreciate it if some one will help me with an alternative or point out my error so that the weeks i spent will ruin in vain. Thanks you as you help. Below is my code.
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)
{
}
}
For C# DOCX file format, you can easily add / edit / iterate over document properties using this C# / VB.NET Word library.
Here is a sample C# code:
// Use the component in free mode. ComponentInfo.SetLicense("FREE-LIMITED-KEY"); // Create new empty document. var document = new DocumentModel(); // Add various built-in document properties. document.DocumentProperties.BuiltIn.Add(BuiltInDocumentProperty.Title, "My document title"); document.DocumentProperties.BuiltIn.Add(BuiltInDocumentProperty.Subject, "My document subject"); document.DocumentProperties.BuiltIn.Add(BuiltInDocumentProperty.Author, "John Doe"); document.DocumentProperties.BuiltIn.Add(BuiltInDocumentProperty.Keywords, "keyword1, keyword2, keyword3"); // Save the document to a file. document.Save("Document.docx", SaveOptions.DocxDefault); // Load a document from the file. document = DocumentModel.Load("Document.docx", LoadOptions.DocxDefault); // Iterate over built-in document properties and write them to Console. foreach (var builtInProperty in document.DocumentProperties.BuiltIn) Console.WriteLine("{0}: {1}", builtInProperty.Key, builtInProperty.Value);
I don''t know if you now need the solution but I do this thing.
My code didn''t work in your version too.
I remove external method "removeproperty" and place all code to "execute_Click" and situation became Ok.
这篇关于在c#中删除/编辑word office builtindocumentproperties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!