Object Model of the Host Application Office VSTO

Object Model of the Host Application Office VSTO

Host Application

A hosted application is any software that is running on another provider’s infrastructure rather than on-premise. These hosted applications are accessed over the internet and have a web- based user interface to be able to interact with. In Microsoft Office the object model of the host application, use the Application field of the This Add In class. This field returns an object that represents the current instance of the host application. The following table lists the type of the return value for the Application field in each VSTO Add-in project.

The following code example shows how to use the Application field to create a new workbook in an VSTO Add-in for Microsoft Office Excel. This example is intended to be run from the ThisAddIn.cs class.

C# code example

Excel.Workbook newWorkbook = this.Application.Workbooks.Add(System.Type.Missing);

VB.Net code example

Dim newWorkbook As Excel.Workbook = Me.Application.Workbooks.Add()

To grab the objects outside the ThisAddIn.cs class, use the Globals. Let’s see example how to do that:

C# code example

Excel.Workbook newWorkbook = Globals.ThisAddIn.Application.Workbooks.Add(System.Type.Missing);

VB.Net code example

Dim newWorkbook As Excel.Workbook = Globals.ThisAddIn.Application.Workbooks.Add()

Application Starts

Not all Office 2010 applications automatically open a document when you start them, and none of the Office 2013 applications open a document when you start them. Therefore, don’t add code in the ThisAddIn_Startup event handler if the code requires a document to be open. Instead, add that code to an event that the Office application raises when a user creates or opens a document. That way, you can guarantee that a document is opened before your code performs operations on it.

The following code example works with a document in Word only when the user creates a document or opens an existing document.

C# code example

private void ThisAddIn_StartupExample(object sender, System.EventArgs e)
{
	this.Application.DocumentOpen +=new Word.ApplicationEvents4_DocumentOpenEventHandler(WorkWithDocument);
	((Word.ApplicationEvents4_Event)this.Application).NewDocument +=new Word.ApplicationEvents4_NewDocumentEventHandler(WorkWithDocument);
}

private void WorkWithDocumentExample(Microsoft.Office.Interop.Word.Document Doc)
{
	try
	{
	Word.Range rng = Doc.Range(0, 0);
	rng.Text = "New Text";
	rng.Select();
	}
	catch(Exception ex)
	{
	// Handle exception if for some reason the document is not available.
	}
}

VB.Net code example

Private Sub ThisAddIn_Startup() Handles Me.Startup
	AddHandler Application.NewDocument, AddressOf WorkWithDocument
End Sub

Private Sub WorkWithDocument(ByVal Doc As Microsoft.Office.Interop.Word.Document) _Handles Application.DocumentOpen
	Dim rng As Word.Range = Doc.Range(Start:=0, End:=0)
	oRang.Text = " New Text "
	oRang.Select()
End Sub

Next>> CustomTaskPanel in Excel Addin

Leave a Reply

Your email address will not be published. Required fields are marked *