请在此代码方面需要您的帮助。我正在用 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)
{
}
}