Windows Phone Developers

Friday, December 5, 2008

How to get the Custom Properties of a Word Document using VSTO

How to get the Custom Properties of a Word Document using .NET (C#)

CustomDocumentProperties returns the entire collection of custom document properties. Use the Microsoft.Office.Core.DocumentProperties.Item(System.Object) property to return a single member of the collection (a Microsoft.Office.Core.DocumentProperties object) by specifying either the name of the property or the collection index (as a number).

private static void WordCustPropertyExample(Word.Application wAP )

{

// Custom Property Extraction For Word Document

Office.DocumentProperties oCusProps = (Office.DocumentProperties)wAP.ActiveDocument.CustomDocumentProperties;

foreach (Office.DocumentProperty oCusProp in oCusProps)

{

// MessageBox.Show( oCusProp.Name);

}

}

To add a custom property to the Word document using VSTO, use the add method as shown below

//Adding a Custom Property for Word Document

oCusProps.Add("CopyEdited By", false, Office.MsoDocProperties.msoPropertyTypeString, "Santa Barbara", false);

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Operator '&' cannot be applied to operands of type 'string' and 'string' - CS0019

Operator '&' cannot be applied to operands of type 'string' and 'string' - CS0019



MessageBox.Show("Hello " & "World");


should be



MessageBox.Show("Hello " + "World");



Try stringbuilder also

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Specify initial values of the array in .NET (C#)

Initial values of the array elements can be specified using an array initializer, which is a list of expressions written between the delimiters { and }

static void ArrayExample()

{

int[] Marks = {56, 78, 45};

int[,] Matrix = new int[5, 5];

string[] Students = {"George Kutty", "Madhavan Nair", "Haneef Iqbal"} ;

for (int i=0;i

{

Console.WriteLine("Student Name: {0} - Marks {1}" , Students[i], Marks[i]);

}

}

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Array – X is a 'variable' but is used like a 'method' Error in .NET (C#) - CS0118

The compiler detected a situation in which a construct was used in some erroneous way or a disallowed operation was tried on a construct. Some common examples include the following:

  • A try to instantiate a namespace (instead of a class)
  • A try to call a field (instead of a method)
  • A try to use a type as a variable
  • A try to use an extern alias as a type.

One possible cause is use of parenthesis ‘()’ in Arrays

static void ArrayExample()

{

int[] Marks = new int[5];

string[] Students = new string[5];

for (int i=0;i

{

Console.WriteLine("Student Name: {0} - Marks {1})" , Students(i), Marks(i));

}

}

To resolve this error, replace parenthesis with square brackets

Console.WriteLine("Student Name: {0} - Marks {1}" , Students[i], Marks[i]);


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Automatic initialization of arrays in .NET (C#)

When you declare an array of particular type, the new operator automatically initializes the elements of an array to their default value, which, for example, is zero for all numeric types and null for all reference types.

The following example shows the automatic initialization of arrays in C#:

static void ArrayExample()

{

int[] Marks = new int[5];

string[] Students = new string[5];

for (int i=0;i

{

Console.WriteLine("Student Name: {0} - Marks {1}" , Students[i], Marks[i]);

}

}

The output will be:

Auto Initialization of Arrays in C#

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Auto-implemented properties in .NET (C#)

Auto-implemented properties are created using set and get accessor without any attributes.

class AccessorExample

{

private string _ProdName;

public string ProdName

{

get

{

return _ProdName;

}

set

{

_ProdName = value;

}

}

public double CostPrice;

//Auto-implemented property

public double discount { get; set; }

//Read-only property

public double EmpDisc { get; private set; }

}

The statement public double discount { get; set; } sets / gets the discount

and can be accessed by

AccessorExample AEx = new AccessorExample();

AEx.ProdName = "T-Shirt";

AEx.CostPrice = 12.34;

AEx.discount = 10;

Console.WriteLine(AEx.ProdName + " Costs " + AEx.CostPrice.ToString() + " before " + AEx.discount + "% discount" );

A property that has both a get accessor and a set accessor is a read-write property, a property that has only a get accessor is a read-only property, and a property that has only a set accessor is a write-only property.

public double EmpDisc { get; private set; } is a read-only property; value of EmpDisc cannot be set. Assigning a value to this property will throw the following exception

AEx.EmpDisc = 15;

The property or indexer 'CSharpSample.AccessorExample.EmpDisc' cannot be used in this context because the set accessor is inaccessible

[The property or indexer 'property/indexer' cannot be used in this context because the set accessor is inaccessible - CS0272]

For auto-implemented properties, the compiler creates a private, anonymous backing field can only be accessed through the property's get and set accessors.





Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Abstract Methods in .NET (C#)

An abstract method is a virtual method with no implementation. The actual implementation of the abstract method will be from a overridden method of the derived class.

An abstract method is declared with the abstract modifier and is permitted only in a class that is also declared abstract.

public abstract class AbsExample {

public abstract string AbsMethod();

}

An abstract method must be overridden in every non-abstract derived class.

class AbsImplementor : AbsExample

{

public override string AbsMethod()

{

return "AbsMethod Implemented!!!";

}

}

The method can be invoked by

AbsImplementor a1 = new AbsImplementor();

Console.WriteLine(a1.AbsMethod());

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon