web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Nishant Rana’s Weblog / Setting Built In and Custom...

Setting Built In and Custom Document Properties using C#

Nishant Rana Profile Picture Nishant Rana 11,325 Microsoft Employee

Create a new windows application project and add a button to it.

On click of that button, we will open a document and add values for a built in and custom document properties.

Than add reference to (Word 10.0 or 11.0 object library) and Microsoft.Office.Core.dll within COM tab of Add reference dialog box.

After adding reference, add this directive

using Microsoft.Office.Interop.Word

using System.Reflection;

Put the following code in the button click event handler

// For optional parameters create a missing object
object missing = System.Reflection.Missing.Value;
// Create an object for filename which is the file to be opened
object fileName = @”C:\MySecond.doc”;
// Create an object of application class
ApplicationClass WordApp = new ApplicationClass();
// open the document specified in the fileName variable
Document adoc = WordApp.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
 
// setting the document built in properties
object oDocBuiltInProps = adoc.BuiltInDocumentProperties;
Type typeDocBuiltInProps = oDocBuiltInProps.GetType();
// setting the Title property with value “My Proposal”
string strIndex = “Title”;
string strValue = “My Proposal”;
typeDocBuiltInProps.InvokeMember(“Item”,
BindingFlags.Default |
BindingFlags.SetProperty,
null, oDocBuiltInProps,
new object[] { strIndex, strValue });
 
 
// setting the custome document properties
object oDocCustomProps = adoc.CustomDocumentProperties;
Type typeDocCustomProps = oDocCustomProps.GetType();
// setting the ProposalSentDate custom date property with current date time
string strIndex1 = “ProposalSentDate”;
string strValue1 = DateTime.Now.ToShortDateString();
object[] oArg = { strIndex1, false, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeDate, strValue1 };
typeDocCustomProps.InvokeMember(“Add”,
BindingFlags.Default |
BindingFlags.InvokeMethod, null,
oDocCustomProps,oArg);
 
WordApp.Visible = true;
 
Byee…


This was originally posted here.

Comments

*This post is locked for comments