Exercise 2: Embedding an Excel Worksheet in Microsoft Word

In this exercise, you will embed your Excel worksheet in a Microsoft Word document. This exercise will also introduce C# developers to optional parameters.

Note:
To verify that each step is correctly performed, it is recommended to build the solution at the end of each task.

Task 1 – Embedding your Excel Worksheet into a New Microsoft Word Document

  1. Open Microsoft Visual Studio 2010 from Start | All Programs | Microsoft Visual Studio 2010 | Microsoft Visual Studio 2010.
  2. Open the OfficeApplication.sln solution file. By default, this file is located in the folder: %TrainingKitInstallFolder%\Labs\Dev10Office\Source\Ex02-EmbeddingWorksheet\Begin\ and choose the language of your preference. Optionally, you can continue working the solution you created in the previous exercise.
  3. Copy your Excel worksheet so that it can be pasted into the Word document. The Copy() method accomplishes this for you. Add the following code to the end of the DisplayInExcel method (in the Visual Basic method, this command must be within the With block):

    Visual Basic

    .Range("A1:C4").Copy()

    C#

    x1.get_Range("A1:C4").Copy();

  4. Now that your worksheet is copied to the clipboard, you need to create a Word document and past the worksheet into it. In the Module1.vb (VB) or Program.cs (C#) file, create a new method called EmbedInWordDocument as shown in the following code:

    (Code Snippet – Office Programmability Lab - Ex2 EmbedInWordDocument VB)

    Visual Basic

    Private Sub EmbedInWordDocument() Dim word As New Word.Application word.Visible = True word.Documents.Add() word.Selection.PasteSpecial(Link:=True, DisplayAsIcon:=False) End Sub

    (Code Snippet – Office Programmability Lab - Ex2 EmbedInWordDocument CSharp)

    C#

    private static void EmbedInWordDocument() { var word = new Word.Application(); word.Visible = true; word.Documents.Add(); word.Selection.PasteSpecial(Link: true, DisplayAsIcon: false); }

    Note:
    C# developers will recognize a new feature to C# 4.0 in the call to PasteSpecial: the use of optional parameters. PasteSpecial actually has seven parameters. In most cases, you do not need to supply values for these parameters, as the defaults are adequate for the application. In previous version of C# these values still had to be provided even if the provided value was of type System.Reflection.Missing.Value, which resulted in called to PasteSpecial that look like this:

    C#

    object iconIndex = System.Reflection.Missing.Value; object link = true; object placement = System.Reflection.Missing.Value; object displayAsIcon = false; object dataType = System.Reflection.Missing.Value; object iconFileName = System.Reflection.Missing.Value; object iconLabel = System.Reflection.Missing.Value; word.Selection.PasteSpecial(ref iconIndex, ref link, ref placement, ref displayAsIcon, ref dataType, ref iconFileName, ref iconLabel);

    Note:
    C# developers should also note that while parameters to PasteSpecial are still by reference, it is no longer necessary to specify this with the ref keyword.

  5. Call the EmbedInWordDocument method from the end of the Main method in the Module1.vb (VB) or Program.cs (C#) file as show:

    Visual Basic

    Sub Main()
    FakePre-e63e899ec8814123a208e74b293f2a12-951dd1b12719412b9ec2908633dbfc58FakePre-308fe8d8db714daba73dce4147705a7d-3bae97c3e93c4f108896d4521699b662FakePre-8026b1500c374288ac41ef967439fdda-1173f8feb48d4151b3e08de78a313622FakePre-ad1ec373f301487f9358a94dcefe400c-fbc2f6cb9d904a91a10bdf3d93b90395FakePre-9660caa835364900bb37be84f27c7a85-91bc2da90f11495d92c978758e9b05bdFakePre-8ce4d0e8fb8d42d8b46c6de4668b502e-937ddf98bc164337b0ad86e6e87f729aFakePre-19cd95569c8641eeaae9eb965b291fa0-a43456cc3c7a4884b1bd5517500ee611FakePre-9668bbe345eb4d94a4953530294c31d7-4442ae72b9af4775a94de1a7cc84158eFakePre-56f688db30cc49139c0522c802e0e641-5e3ca282992e48c18eb11a0238911506FakePre-4f1ebe063e0943e9a620484ddd4f7a82-1b4bac3eddca43cda4798c38a4999189FakePre-29fd75b30693411a9c1c943880e05ac6-c02f572f3871416292e68583209f26f3FakePre-4ff7895e28174a22a65bca4796726480-37ab3067ff1248a0ad60c4ebbfe83002FakePre-df513442512846f3bd76742056ac6333-2c61689445a446afb9fd0eac99773485 EmbedInWordDocument()FakePre-64919597418c4a5581eb46f3afabad62-e74d4febdee54754b5fdd1d8a7302fad

    C#

    static void Main(string[] args)
    FakePre-753dbd7049b9410e9f64b032ec1de81c-94842b592227431fbd4681726016670fFakePre-c2e0fb391bab4195b4460df26a469432-0db0303c8d49412088831667c77ba4fdFakePre-14e526970e65467bb75aebb225149a8a-653bf528c1ac4647a69738d45dc00a00FakePre-dcd4fe2af6d844dca97ed9e2f6237c90-6b822056db3b4f7d84116c35304efcb6FakePre-59e49b1165eb483cb4fad3bac6b0975e-cfee049fd2274f7f83467c255174a1c8FakePre-037d997f4d664fbbaad1ecfebd9eff11-96cd642b450740fd91f77ef1ca28cdf7FakePre-620215c246ff4222bb8844212e3a739e-e4ed6470cf324712b6fc6c233ae21d8cFakePre-f75a21afd8a641ee8ec15303a77072d7-31bf465cec0b4497a1d5cce9ea53a311FakePre-39ec6e80add44fda9f008220190dcf27-0baf927b86e84ab1a6ea203f3e3a5b25FakePre-fac449c8c5ed4fe88f0ba2f7ba289e52-a925adef9300434ba4377ef81fa40952FakePre-ad68bfc81d344dba808424a80c1cb369-891919890b0f406191d09fae3c30fd90FakePre-cb9353dee41942f38afd7aa8515f3aa4-80c3984990b14f11a7d36ab8466b3e2eFakePre-9cdca55a26cd49f59b793f5ad717a5c4-2c7009ea6b6243e788a6be50172770abFakePre-fe3b7066e6484cdabc346d185c33ab9f-f4eda3bb84704305a5b96272119d08dfFakePre-42b11090b86e44a98b08e3433502e74b-bbc4a22b32ec429389a96b046f28eec5FakePre-73d9e8ad4677409589fcc65f3f2514fa-729320bab32749a9a5b1d48b33bd201fFakePre-3db2066ccce84aaeaa0e8ef84aafeebb-baf1f8dcb390487486904230be4065a2 EmbedInWordDocument();FakePre-1facc2b2100841f8b359856c73206ea9-7ffbaedce5c146a3b1ee6133bc901839

Next Step

Exercise 2: Verification