Opening and inserting a picture in word document programmatically 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 insert a picture to it.

(In the doc file insert a table at the location where you want the picture to appear.)

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

After adding reference, add this directive

using Microsoft.Office.Interop.Word

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);

// We can insert the picture using Range objects AddPicture method

// To insert a picture at a particular location in the word document

// insert a table over there and then refer that location through range object

Range rngPic = adoc.Tables[1].Range;

// we can even select a particular cell in the table

//Range rngPic = rng.Tables[1].Cell(2, 3).Range;

rngPic.InlineShapes.AddPicture(@”C:\anne_hathaway.jpg”, ref missing, ref missing, ref missing);

WordApp.Visible = true;

 

Bye

 

Inserting or appending document or file in a word document programmatically using C#


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

On click of that button, we will create a new document and append or insert content of two documents in it.

First add reference to (Word 10.0 or 11.0 object library) within COM tab of Add reference dialog box.

After adding reference, add this directive

using Microsoft.Office.Interop.Word

Put this code on button click

// For optional parameters create a missing object

object missing = System.Reflection.Missing.Value; 

// Create an object of application class

ApplicationClass WordApp = new ApplicationClass();  

// add a document in the Application

Document adoc=WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing); 

// declare variables for setting the position within the document

object start = 0;

object end = 0; 

// create a range object which starts at 0

Range rng = adoc.Range(ref start, ref missing);

// insert a file

rng.InsertFile(@”C:\MyFirst.doc”, ref missing, ref missing, ref missing, ref missing);

// now make start to point to the end of the content of the first document

start = WordApp.ActiveDocument.Content.End – 1; 

// create another range object with the new value for start

Range rng1 = adoc.Range(ref start, ref missing);

// insert the another document

rng1.InsertFile(@”C:\MySecond.doc”, ref missing, ref missing, ref missing, ref missing);

// now make start to point to the end of the content of the first document

start = WordApp.ActiveDocument.Content.End – 1; 

// make the word appliction visible

WordApp.Visible = true;

 

Bye

Upgrade or Using CRM 3 callouts in CRM 4


Hi,

These are the steps we followed for using crm 3.0 callouts in crm 4.0

In one case we upgraded our crm 3.0 to crm 4.0,

We had to put our crm 3.0 callouts in the following path

C:\Program Files\Microsoft CRM\Server\bin\assembly

We also had to put Microsoft.Crm.Platform.Callout.Base in the GAC.

Followed by an IISRESET

In second case, we had a fresh installation of  CRM 4.0.

We had to put our crm 3.0 callouts in the following path

C:\Program Files\Microsoft Dynamics CRM\Server\bin\assembly.

Followed by an IISRESET.

And things worked fine for us!!!

Passing Parameters to Workflow from Host Application `


To pass parameter to Workflow.

First we need to create a local variable and property in the workflow class.

private string _message = string.Empty;

public string Message

{

get { return _message; }

set { _message = value; }

}

From our hosting application to pass the value for the parameter,

we need to create a generic Dictionary object which uses String for key and Object as value for the parameter.

Key for parameter should exactly match the public property in the workflow.

Dictionary<String, object> wfParam = new Dictionary<string, object>();

wfParam.Add(“Message”, “My Hello World”);

Now we need to pass this Dictionary object to the workflow using one of the overloaded method for CreateWorkflow

WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(HelloWorldWorkflow.Workflow1),wfParam );

Bye…

Understanding a Hello World program in Windows Workflow Foundation


Hi,

1. Create a new Sequential Workflow Console Application project.

2. Open Workflow1.cs in design mode.

3. Drag a Code activity to the workflow at (Drop activities to create a sequential workflow)

4. Select Code activity and in the properties window for ExecuteCode property write MyMessage and the press enter twice, which will create a method with the same name.

private void MyMessage(object sender, EventArgs e)
{
Console.Write(“Hello World”);
Console.ReadLine();
}

5. Press F5 to run the project.

This completes our first program in windows workflow foundation.

What we have done over here is that

We have created a console application which is hosting a workflow. Workflow requires an application as a host. It could be a console application, windows forms application,

asp.net application and even a Windows service.

We can found the code used for hosting the workflow in Program.cs.

Let’s try to understand the code

// First we are creating an instance of WorkflowRuntime

// WorkflowRuntime – is responsible for starting our workflow, firing events for different situations

// while the task is executing.

using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())

{

// Our application and the workflow, both are running on different threads.

// We need our application to wait long enough before our workflow either gets completed or terminated(due to error).

// For this AutoResetEvent is used.

// Here an instance of AutoResetEvent is created i.e. waitHandle

// calling waitHandle.WaitOne() will make the main thread to wait until it is signalled using

// waitHandle.Set(), which will be done when workflow either gets complete or gets terminated.

AutoResetEvent waitHandle = new AutoResetEvent(false);

 

// Here anonymous method is used to declare an event handler for WorkflowCompleted event

// Once workflow gets completed, the main thread is signalled to proceed further through

// waitHandle.Set()

workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e)

{waitHandle.Set();};

 

// Here anonymous method is used to declare an event handler for WorkflowTerminated event

// Once workflow gets completed, the main thread is signalled to proceed further through

// waitHandle.Set(), which releases the console application from its wait

workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)

{

Console.WriteLine(e.Exception.Message);

waitHandle.Set();

};

 

// Workflow instance is composed of one or more activities. WorkflowRuntime executes these workflow instances.

// workflowRuntime.CreateWorkflow->Creates a workflow instance and if the workflow runtime hasn’t been started

// the CreateWorkflow method calls StartRuntime method of WorkflowRuntime

WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(HelloWorldWorkflow.Workflow1));

 

//Starts the execution of the worflow instance

instance.Start();

 

// The main thread waits till it is signalled to proceed further i.e. waitHandle.Set()

waitHandle.WaitOne();

}

Bye…

How to – Close an opportunity programmatically CRM


We can make use of following for closing the opportunity programmatically in CRM.

WinOpportunityRequest for closing an opportunity as won

//We create an instance of WinOpportunityRequest class as salesstage is Won.
WinOpportunityRequest woReq=new WinOpportunityRequest();
//WinOpportunityRequest takes 2 values: Opportunityclose and Status.
opportunityclose oc=new opportunityclose();
//Since Opportunityid is of type Lookup, we create an instance of the Lookup class. Lookup class has 2 attributes: Lookup.Value and Lookup.Type
Lookup lookup=new Lookup();
//We pass the GUID value of the opportunity to an instance of the Lookup class.
lookup.Value=entityContext.InstanceId;                                                                                          //We specify the type of the entity being passed.
lookup.type=EntityName.opportunity.ToString();
oc.opportunityid=lookup;
oc.actualend=actualclosedate(CrmDateTime variable);
oc.actualrevenue=new_estimatedrevenue(CrmMoney variable);
woReq.OpportunityClose=oc;

//The Status parameter corresponds to Status Reason in the Microsoft CRM application. If you pass -1 for this parameter, the platform sets the status to the appropriate value for the Microsoft CRM application.
woReq.Status=-1;
//The WinOpportunityRequest is sent to the platform using the Execute method. The platform will run the request and send back an instance of the Response class message.
WinOpportunityResponse woRes=(WinOpportunityResponse)service.Execute(woReq);


LoseOpportunityRequest for closing opportunity on lost and dropped
LoseOpportunityRequest loReq=new LoseOpportunityRequest();
//LoseOpportunityRequest takes 2 values: Opportunityclose and Status.
opportunityclose oc=new opportunityclose();
//Since Opportunityid is of type Lookup, we create an instance of the Lookup class. Lookup class has 2 attributes: Lookup.Value and Lookup.Type
Lookup lookup=new Lookup();
//We pass the GUID value of the opportunity to an instance of the Lookup class.
lookup.Value=entityContext.InstanceId;
//We specify the type of the entity being passed.
lookup.type=EntityName.opportunity.ToString();
oc.opportunityid=lookup;
oc.actualend=actualclosedate;
oc.actualrevenue=new_estimatedrevenue;
loReq.OpportunityClose=oc;
//The Status parameter corresponds to Status Reason in the Microsoft CRM application. If you pass -1 for this parameter, the platform sets the status to the appropriate value for the Microsoft CRM application.
loReq.Status=-1;
//The LoseOpportunityRequest is sent to the platform using the Execute method. The platform will run the request and send back an instance of the Response class message.
LoseOpportunityResponse loRes=(LoseOpportunityResponse)service.Execute(loReq);

Bye

Nishant Rana's Weblog

Everything related to Microsoft .NET Technology

Skip to content ↓