Setting Built In and Custom Document Properties using C#


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…


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

One thought on “Setting Built In and Custom Document Properties using C#”

  1. if you have any problem at
    // Create an object of application class
    ApplicationClass WordApp = new ApplicationClass();

    You may throw away with this,

    using Word = Microsoft.Office.Interop.Word;

    // Create an object of application class
    Word.Application WordApp = (Word.Application)Activator.CreateInstance(Type.GetTypeFromProgID(“Word.Application”));

    // open the document specified in the fileName variable
    Word.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);

    Like

Leave a reply to Raveendra Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog - PPUG.ORG

AI - Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.