Sunday, May 16, 2010

Simple Linq Sample in Word Vsto

Using Linq In Word Vsto The Below Sample Shows very Basic use of Linq with VSTO word.

Requirement:-

Vs 2008
Vsto

Code:-

private void ThisDocument_Startup(object sender, System.EventArgs e)
{
Linquse_InWord_Vsto("Avinash Tiwari Linq Sample");
}

public void Linquse_InWord_Vsto(String strText)
{

var qChars = from c in strText select c;
int charCount = qChars.Count();

List textWords = new List(strText.Split(new char[] { ' ' }));
var qLetterWords = textWords.FindAll(x => (x.Length >= 2));

this.Application.Selection.InsertAfter("\n Character Count: " + charCount + "\n Word Count: " + qLetterWords.Count);

this.Application.ActiveDocument.Content.InsertBefore(strText);


}

Output:-



Saturday, May 15, 2010

Simple Database Binding In Vsto2007

Database Binding in VSTO with Word..

Requirement:-

Vs(2008)
Vsto Tool
Sqlserever
and Interest


Create Database Myprofiletest having Follwing Fileds..

Srno
Name
Sex
Address
Profession

Know Let Start With Visual Studio

1}


Create Simple Table in word which have Name,Address,Sex,Profession and put as header in Table TD.


2}


Know select Datasource from Data menu from Top.

3}

When u Click datasource u will see 3 option

4}

Know select Database and complete Procedure to Connecting and Creating Connection String

5}

Once u connected properly u Given Oprtion Select select Table u have Created

6}

U will Filed in Datasoursce Tab it will map it self with control example if text it will Give Text and datTime then Picker Control.. Etc

7}

Drag and Drop The filed where u want

8}


When u Run Output u will See..

9}

if want Chk Some and put message and on Event of That

private void plainTextContentControl2_Entering(object sender, Microsoft.Office.Tools.Word.ContentControlEnteringEventArgs e)
{
// Display the dialog window for the edit operation
System.Windows.Forms.DialogResult myResult = new DialogResult();
// Message with Yes/No option to proceed
myResult = MessageBox.Show("Do you want to UnLock?", " Address?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.DefaultDesktopOnly, false);
// If you select the no it will remain in lock mode and display the content
if (myResult == DialogResult.Yes)
plainTextContentControl2.LockContentControl = false;
}

chk the above code










VSTo WORD 2007 WITH WORD ADDIN

Using Simple Task pane in Word Addin Sample..

Requirement:-

Interest
Vs2008
and Vsto

Code:-

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
AddinSample1();
}


//Initlizating the customtaskpane object of the current application
private Microsoft.Office.Tools.CustomTaskPane PacktTaskPaneControl = null;

//Inializing the Usercontrol
UserControl PacktUsercontrol = new UserControl();


public void AddinSample1()
{
//add the TextBox control to the CustomTaskPaqne
//The PacktUserControl parameter sets the title to search
PacktTaskPaneControl = this.CustomTaskPanes.Add(PacktUsercontrol, "Sample Avinash Search");

// Set the CustomTaskPane to visible
PacktTaskPaneControl.Visible = true;



}

Output:-





Word addin with task pane






Code :-

User Control



WordAddIn1.ThisAddIn newtest = new WordAddIn1.ThisAddIn();

int mytestvalue = 0;
mytestvalue = Convert.ToInt16(textBox1.Text) + Convert.ToInt16(textBox2.Text);
string Myvalue = "";
Myvalue = Convert.ToString(mytestvalue);

MessageBox.Show(Myvalue);


Addin Control...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
using Microsoft.Office.Tools.Word.Extensions;
using System.Windows.Forms;

namespace WordAddIn1
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
AddinSample1();
}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}

//Initlizating the customtaskpane object of the current application
private Microsoft.Office.Tools.CustomTaskPane PacktTaskPaneControl = null;

//Inializing the Usercontrol
UserControl PacktUsercontrol = new UserControl();


public void MyTest(string Mytest)
{
// this.Content.InsertAfter(Mytest);

}


public void AddinSample1()
{
//add the TextBox control to the CustomTaskPaqne
//The PacktUserControl parameter sets the title to search
PacktTaskPaneControl = this.CustomTaskPanes.Add(new UserControl1(), "Sample Cal Search");

// Set the CustomTaskPane to visible
PacktTaskPaneControl.Visible = true;



}


#region VSTO generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}

#endregion
}
}









ACTION PANE IN VSTO 2007 (Word)

Some Simple Code Snippet Which I have learned in process of Learing Vsto .

Requirment:-

Interest
Vs2008
and Vsto


Code :-

private void ThisDocument_Startup(object sender, System.EventArgs e)
{
CreateActionPane();
}



//Initlizating the TextBox control to use in Action pane
TextBox VstoTextBox = new TextBox();
public void CreateActionPane()
{

//set text property for TextBox Control

VstoTextBox.Text = "Avinash in Action Pane User";

//Add the TextBox control to ActionPane
ActionsPane.Controls.Add(VstoTextBox);

//On document load ActionPane is shown
ActionsPane.Show();
}

OutPut:-


//Adding DatePicker in Action Pane

Code :-

//initalizing the datetimepicker control
DateTimePicker _packtdatetimer = new DateTimePicker();
public void datetimepickersample()
{
this.ActionsPane.Controls.Add(_packtdatetimer);
}



void _packtdatetimer_ValueChanged(object sender, EventArgs e)
{
//Read content and inseret the value after the paragraph
this.Content.InsertParagraphAfter();

//Insert value from the DateTimepicker select value..
this.Content.InsertAfter(_packtdatetimer.Value.ToString());
}

//Modify Internal Start Up

private void InternalStartup()
{
//value chnaged event registration in the internalStartup of the application

this._packtdatetimer.ValueChanged += new EventHandler(this._packtdatetimer_ValueChanged);

this.Startup += new System.EventHandler(ThisDocument_Startup);
this.Shutdown += new System.EventHandler(ThisDocument_Shutdown);
}



OutPut:-






Some Simple Code Snippet for Vsto 2007 (Word)

While Learning Vsto With Word I have came Some simple Code snippet which may come handy.
When The Real programming Begins ..

Requirment :-

Interest with
Vs2008
and Vsto

Let Start

1} Inserting Text in Word 2007


InsertAfter method inserts text at the end of the active range or selection, whereas InsertBefore inserts text at the start of the active range or selection.

Code :-

// Using InsertBefore method inserts text
this.Application.ActiveDocument.Content.InsertBefore("Avinash @ The Start-");

//using Insert method insert text
this.Application.ActiveDocument.Content.InsertAfter(" - Avinash @ the End ");

Same Opertion Can be Performed using Selection Object

// Using Selection Object inserting text after the text
this.Application.Selection.InsertAfter("Avinash @ The End");

// Using Selection Object inserting text before the text

this.Application.Selection.InsertBefore("Avinash @ The Start");

2} Selecting text in a Word 2007 document


///Intializing the range object
Word.Range PackRangeSelect;
////check the sentence count
if (this.Sentences.Count >= 1)
{
////set the start and end point has object
object pktStartfrom = this.Sentences[2].Start;
object pktStopHere = this.Sentences[5].End;
////assign the selection range
PackRangeSelect = this.Range(ref pktStartfrom, ref pktStopHere);

////select the sentence using select() Method
PackRangeSelect.Select();
}
else
{
return;
}

OutPut



3} Creating Table in Word



//Object instance
object pktMissing = System.Type.Missing;

// Range on the application selection

Word.Range PacktRangePresent = this.Application.Selection.Range;

// Using Table object add in the Word document

Word.Table PacktTable = this.Application.ActiveDocument.Tables.Add(PacktRangePresent, 3, 4, ref pktMissing, ref pktMissing);

// Border propety of the Table we are creating

Word.Border[] PacktBorder = new Word.Border[6];
PacktBorder[0] = PacktTable.Borders[Word.WdBorderType.wdBorderLeft];
PacktBorder[1] = PacktTable.Borders[Word.WdBorderType.wdBorderRight];
PacktBorder[2] = PacktTable.Borders[Word.WdBorderType.wdBorderTop];
PacktBorder[3] = PacktTable.Borders[Word.WdBorderType.wdBorderBottom];
PacktBorder[4] = PacktTable.Borders[Word.WdBorderType.wdBorderHorizontal];
PacktBorder[5] = PacktTable.Borders[Word.WdBorderType.wdBorderVertical];
// Border formatting of the Table
// Loop through the border and set color for table
foreach (Word.Border pktBorder in PacktBorder)
{
// Table line style propety
pktBorder.LineStyle = Word.WdLineStyle.wdLineStyleTriple;
// Table line color property
pktBorder.Color = Word.WdColor.wdColorGray30;
}

Output:-

Saturday, May 8, 2010

Open XML Hello World

Currently I am Involved In Open Xml project.. I was Hard to Find Some Good resource regarding OpenXml So I taught I Should Create My Own So I have..

Requirement (What I Have used):-

OpenXML SDK 2.0
Visual Studio 2008

Let Start:-

Add Some Assemblies

DocumentFormat.OpenXml
Window.Base
System.Xml
System.IO
System.IO.Packaging

Download OpenXMl SDK..



ADD
DocumentFormat.OpenXml

Window.Base
Assemblies




CODE WHAT I HAVE USED

Assemblies

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Packaging;
using System.IO;
using System.Xml;

Creating Package Method..

public void CreatePackage()
{
//File Would Create in D Drive With name Sample.docx
using (Package package = Package.Open("D://Sample.docx", FileMode.Create))
{

PackagePart mainPart = package.CreatePart(
new Uri("/Words/document.xml", UriKind.Relative),"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");

//This Method Which has Code Below
XMLFile(mainPart);


PackageRelationship mainRelationship = package.CreateRelationship(
mainPart.Uri,
TargetMode.Internal,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument");


}

}
XMLFile METHOD


public void XMLFile(PackagePart mainPart)
{
string xmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
using (XmlWriter writer = XmlWriter.Create(mainPart.GetStream(FileMode.Create, FileAccess.ReadWrite)))
{
writer.WriteStartDocument();
writer.WriteStartElement("w", "document", xmlNamespace);
writer.WriteStartElement("w", "body", xmlNamespace);
writer.WriteStartElement("w", "p", xmlNamespace);
writer.WriteStartElement("w", "r", xmlNamespace);
writer.WriteStartElement("w", "t", xmlNamespace);
writer.WriteString("Hello World");
writer.WriteEndDocument();
}

}

OutPut Of Code


Opening Docx File In WinRar

Extraction DOCX file in Folder


Word folder Is Created Becuause be Defined In code

new Uri("/Words/document.xml", UriKind.Relative)

Now Going To Document.xml


Now Checking Document.xml


This is all about OpenXMl Hello World
Hope It Helps Some Body To Understand