Showing posts with label office. Show all posts
Showing posts with label office. Show all posts

Sunday, December 19, 2010

how to Office Developers is called No-PIA

This sample shows how to use the C# 4.0 features called COM Interop, omitting ref, indexed properties and Named and Optional Parameters to create a C# application that communicates with Microsoft Office. C# developers have traditionally had to write relatively verbose code in order to access Microsoft Office applications such as Word or Excel. New C# 4.0 features make it much simpler to call Office APIs.

Consider this declaration for a Microsoft Office method used in this sample:

void PasteSpecial(ref object IconIndex = null, ref object Link = null,
ref object Placement = null, ref object DisplayAsIcon = null,
ref object DataType = null, ref object IconFileName = null,
ref object IconLabel = null);

As you can see, this method takes a fairly large number of parameters. In C#, developers have traditionally had to fill out each parameter, even though the developers of this call had intended to simplify its use by supporting optional parameters. In C# 4.0, the new support for named and optional parameters allows the developer to specify only the parameters of interest, and to take default values for the other parameters:

word.Selection.PasteSpecial(Link: true, DisplayAsIcon: true);

In the call to the PasteSpecial method the Link and DisplayAsIcon parameters are explicitly named, and set to the value true. All the other parameters default to values specified internally by the developers of the Office API, as shown in the above signature.

You can create your own calls that support named and optional paramters. Consider this example:

public void M(int x, int y = 5, int z = 7) { }

In this method, the parameters y and z are assigned default values. Calls to this method might look like this:

M(1, 2, 3); // ordinary call of M
M(1, 2); // omitting z – equivalent to M(1, 2, 7)
M(1); // omitting both y and z – equivalent to M(1, 5, 7)
M(1, z: 3); // passing z by name
M(x: 1, z: 3); // passing both x and z by name
M(z: 3, x: 1); // reversing the order of arguments

A new dynamic feature in C# 4.0 makes Office much easier for C# developers to use. Types used in Office are now presented to C# developers as if they were declared with the type dynamic. Here is the traditionally way to set a Cell property:

((Excel.Range)excel.Cells[1, 1]).Value2 = "ID";

In C# 4.0 developers can now write code that looks like this:

X1.Cells[1, 1].Value = "ID";

A feature called Index Properties allows us to simplify the call further, so that it looks like this:

xl.Cells[1, 1] = "ID";

A final feature of interest to Office Developers is called No-PIA. Primary Interop Assemblies are generated from COM interfaces and provide helpful type support at design time. At runtime, however, they increase the size of your program, and can cause versioning issues. The No-PIA feature allows you to continue to use PIAs at design but omit them at runtime. The C# compiler will bake the small part of the PIA that a program actually uses directly into its assembly. You will no longer need to include PIA's in the distribution of your programs.


// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
//

using System;
using System.Collections.Generic;

using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;

public class Account
{
public int ID { get; set; }
public double Balance { get; set; }
}

public class Program
{
static void Main(string[] args)
{
var checkAccounts = new List<Account> {
new Account {
ID = 345,
Balance = 541.27
},
new Account {
ID = 123,
Balance = -127.44
}
};

DisplayInExcel(checkAccounts, (account, cell) =>
{
// This multiline lambda will set
// custom processing rules.
cell.Value = account.ID;
cell.Offset[0, 1].Value = account.Balance;

if (account.Balance < 0)
{
cell.Interior.Color = 255;
cell.Offset[0, 1].Interior.Color = 255;
}
});

var word = new Word.Application();
word.Visible = true;
word.Documents.Add();
word.Selection.PasteSpecial(Link: true, DisplayAsIcon: true);
}

public static void DisplayInExcel(IEnumerable<Account> accounts,
Action<Account, Excel.Range> DisplayFunc)
{
var xl = new Excel.Application();

xl.Workbooks.Add();
xl.Visible = true;
xl.Cells[1, 1] = "ID";
xl.Cells[1, 2] = " Balance";
xl.Cells[2, 1].Select();
foreach (var ac in accounts)
{
DisplayFunc(ac, xl.ActiveCell);
xl.ActiveCell.Offset[1, 0].Select();
}

xl.Range["A1:B3"].Copy();
//xl.get_Range("A1:B3").Copy();

xl.Columns[1].AutoFit();
xl.Columns[2].AutoFit();
}
}

Monday, April 12, 2010

Outlook Personal Folders File Format

Intellectual Property Rights Notice for Open Specifications Documentation

* Technical Documentation. Microsoft publishes Open Specifications documentation for protocols, file formats, languages, standards as well as overviews of the interaction among each of these technologies.

* Copyrights. This documentation is covered by Microsoft copyrights. Regardless of any other terms that are contained in the terms of use for the Microsoft website that hosts this documentation, you may make copies of it in order to develop implementations of the technologies described in the Open Specifications and may distribute portions of it in your implementations using these technologies or your documentation as necessary to properly document the implementation. You may also distribute in your implementation, with or without modification, any schema, IDL’s, or code samples that are included in the documentation. This permission also applies to any documents that are referenced in the Open Specifications.

* No Trade Secrets. Microsoft does not claim any trade secret rights in this documentation.

* Patents. Microsoft has patents that may cover your implementations of the technologies described in the Open Specifications. Neither this notice nor Microsoft's delivery of the documentation grants any licenses under those or any other Microsoft patents. However, a given Open Specification may be covered by Microsoft's Open Specification Promise (available here: http://www.microsoft.com/interop/osp) or the Community Promise (available here: http://www.microsoft.com/interop/cp/default.mspx). If you would prefer a written license, or if the technologies described in the Open Specifications are not covered by the Open Specifications Promise or Community Promise, as applicable, patent licenses are available by contacting iplg@microsoft.com.

* Trademarks. The names of companies and products contained in this documentation may be covered by trademarks or similar intellectual property rights. This notice does not grant any licenses under those rights.

* Fictitious Names. The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted in this documentation are fictitious. No association with any real company, organization, product, domain name, email address, logo, person, place, or event is intended or should be inferred.

Reservation of Rights. All other rights are reserved, and this notice does not grant any rights other than specifically described above, whether by implication, estoppel, or otherwise.

Tools. The Open Specifications do not require the use of Microsoft programming tools or programming environments in order for you to develop an implementation. If you have access to Microsoft programming tools and environments you are free to take advantage of them. Certain Open Specifications are intended for use in conjunction with publicly available standard specifications and network programming art, and assumes that the reader either is familiar with the aforementioned material or has immediate access to it.

http://msdn.microsoft.com/en-us/library/ff385210.aspx

Shared Cache - .Net Caching made easy

All information about Shared Cache is available here: http://www.sharedcache.com/. Its free and easy to use, we provide all sources at codeplex.

Facebook Badge