1: private void button1_Click(object sender, EventArgs e)
2: {
3: }
4:
5: With:-
6:
7: private void button1_Click(object sender, EventArgs e)
8: {
9: Word.Application oWord;
10: Word._Document oDoc;
11: object oMissing = Missing.Value;
12: object oDocBuiltInProps;
13: object oDocCustomProps;
14: object fileName = @"C:\test.doc";
15:
16: //Create an instance of Microsoft Word and make it visible.
17: oWord = new Word.Application();
18: oWord.Visible = true;
19:
20: //Create a new Document and get the BuiltInDocumentProperties collection.
21: oDoc = oWord.Documents.Open(ref fileName,ref oMissing,ref oMissing,
22: ref oMissing,ref oMissing,ref oMissing,
23: ref oMissing,ref oMissing,ref oMissing,
24: ref oMissing,ref oMissing,ref oMissing,
25: ref oMissing,ref oMissing,ref oMissing,
26: ref oMissing);
27: oDocBuiltInProps = oDoc.BuiltInDocumentProperties;
28: Type typeDocBuiltInProps = oDocBuiltInProps.GetType();
29:
30: //Get the Author property and display it.
31: string strName;
32: string strValue;
33:
34: //Add a property/value pair to the CustomDocumentProperties collection.
35: oDocCustomProps = oDoc.CustomDocumentProperties;
36: object oDocAssemblyNameProp;
37: Type typeDocAssemblyNameProp;
38: Type typeDocCustomProps = oDocCustomProps.GetType();
39: try
40: {
41: strName = "_AssemblyName";
42: oDocAssemblyNameProp = typeDocCustomProps.InvokeMember("Item",
43: BindingFlags.Default | BindingFlags.GetProperty,
44: null, oDocCustomProps, new object[] { strName });
45: typeDocAssemblyNameProp = oDocAssemblyNameProp.GetType();
46: strValue = typeDocAssemblyNameProp.InvokeMember("Value",
47: BindingFlags.Default | BindingFlags.GetProperty,
48: null, oDocAssemblyNameProp, new object[] { }).ToString();
49: }
50: catch
51: {
52: strName = "_AssemblyName";
53: strValue = "*";
54: object[] oArgs = { strName, false, MsoDocProperties.msoPropertyTypeString, strValue };
55: typeDocCustomProps.InvokeMember("Add",
56: BindingFlags.Default | BindingFlags.InvokeMethod, null,
57: oDocCustomProps, oArgs);
58: }
59: try
60: {
61: strName = "_AssemblyLocation";
62: oDocAssemblyNameProp = typeDocCustomProps.InvokeMember("Item",
63: BindingFlags.Default | BindingFlags.GetProperty,
64: null, oDocCustomProps, new object[] { strName });
65:
66: typeDocAssemblyNameProp = oDocAssemblyNameProp.GetType();
67: strValue = typeDocAssemblyNameProp.InvokeMember("Value",
68: BindingFlags.Default | BindingFlags.GetProperty, null,
69: oDocAssemblyNameProp, new object[] { }).ToString();
70: }
71: catch
72: {
73: strName = "_AssemblyLocation";
74: strValue = @"C:\WordDocument.application"; ;
75: object[] oArgs = { strName, false, MsoDocProperties.msoPropertyTypeString, strValue };
76: typeDocCustomProps.InvokeMember("Add",
77: BindingFlags.Default | BindingFlags.InvokeMethod,
78: null, oDocCustomProps, oArgs);
79: }
80:
81: oDoc.Save();
82: oDocBuiltInProps = null;
83: oDocCustomProps = null;
84: oDoc.Close(ref oMissing, ref oMissing,ref oMissing);
85: oDoc = null;
86: oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
87: oWord = null;
88:
89: }