2012-04-10 3 views
1

このコードに関して助けが必要です。私は、C#でコードを作成しています。これは、プロパティで構築された単語ドキュメントをクリアし、提供されている置換えで置き換えることができます。私の例では、私はmicrosftサポートウェブサイト http://support.microsoft.com/kb/303296のオンラインで見つかった、私のコードは問題なく、私はどんなコンパイルエラーも取得しないで動作すると思います。何の結果も得られないので、私はそれを求めて何をしていません。何人かがulternativeで私を助けたり、私が過ごした週が無駄に破棄されるように私のエラーを指摘すれば、皆さん、本当に感謝します。助けてくれてありがとう。以下は私のコードです。Cでのワードオフィスの組み込みドキュメントのプロパティの削除/編集

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) 
       { 

       } 


} 
+0

なんてこった!まだそれは仕事を得ることができません。私は狂っている。いくつかのpls助け.. – brickleberry

答えて

1

として、あなたはそうのような名前で単語のプロパティを変更することができ、hereが見つかりました:

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"、...

2

1)の参照を追加しますようにMicrosoftに.CSharp。

2)を取得は、BuiltInDocumentProperties:

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; 
} 
関連する問題