Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
I want to show you some other ways to use COM Interop. In this movie, I'll look at interacting with Microsoft Excel and show you how to use the Windows zdhell command. I'm inside Visual Studio, I've opened the UsingComInterop project, and I'm going to be working with this XAML file right here, ExcelInterop.xaml. The idea is I'm going to take these values from these text boxes and put them into a spreadsheet, pick a chart from this combo box dropdown, and when I click the Create Chart button, I'll make that chart in the Excel spreadsheet.
Let me show you the code. I'll double-click on the C# file. I'll put a break point--it looks like I already have one here on line 39--and then I'm going to run the application. So I'll press F5 to run the application, and then I'm going to go down to my taskbar and show you that Excel is currently not running. I'll then click the Create Chart button, hit my break point. I have verified that I'm able to use the AutomationFactory, and then I'll come down here, and I'm going to use the progID for Excel, right here, Excel.Application, to instantiate Excel and open it up.
Then I'm going to make it visible so we can see it. I'll say xlApp.Visible = true, and now if you look on my taskbar, you can see that there's Microsoft Excel. Now it's empty. There's nothing in it yet, but I'm going to create a workbook here in just a second. That's what I'm going to do on line 46, so I'll press F11 to step through my code. And then down here, I'm going to add some text to some of the cells, the number of the cells, and then when I get down here, I'm going to stop and show you what I've got in my spreadsheet.
So currently, I have some titles, R and B, Rock, Jazz, and the numbers. These numbers are coming from those text boxes. Okay, next I'm going to grab a range of cells. I'm going to start at Cell(1, 1) and go through Cell(2, 5). Then I'm going to create a chart. Notice I'm using the C# dynamic keyword here. And then I'm going to call the chart.ChartWizard to generate actual chart, and then I'll figure out which chart you've selected from the combo box. I've picked the default. I'll then create a ChartId that sets the ChartType here, and then pick a style, and then we'll switch over to Excel and see that I have a page with a chart, my three pieces of data, my titles, and then you can see it also added a tab here.
Here's the original data; here's my chart tab. Very powerful! Very exciting! If you already know how to do COM Interop for Excel or Word or Outlook or any of the other Office tools, that code works exactly the same in Silverlight now. Switch back to Visual Studio. Next, I'm going to close the workbook and then down here on line 89, I'm going to quit the process, which will shut down the Excel application process. So I'll press F5 and you'll see that that Excel is now gone from the taskbar.
Another kind of automation you can do with COM is something called shell automation. That enables access to features of the Windows shell. This includes things like displaying standard dialogs, arranging windows on the screen, starting and stopping applications, starting and stopping services. My favorite function in the shell is the Run function. With the Run function, I can do things like run an executable, modify registry keys, or manipulate documents. The code for this is over here in this UseWScript.xaml.
You'll see why I called it that in just a second. I need to tell Silverlight to start with this page, not the ExcelInterop page. So I'm going to go to my App.xaml.cs and find line 29 and change this to say UseWScript. And then I'm going to go open this UseWScript.xaml, find the code behind page to open that, and here's why it's called WScript. The way you get progID for the shell is WScript.Shell. So this is saying create an instance of the shell, and then I'm going to call shell.Run.
This is the same as going over to Start and typing in the path to that executable. It's going to run Notepad and then when it's up and running, it's going to call shell.SendKeys, which will send the string over to Notepad. Let's see if that works. Press F5 to start. Then I click on the Run WScript.Shell button, and then you see my string being passed in via the SendKeys method. Now the possibilities for integration with a Windows application in Silverlight is endless.
This is really exciting news for the Silverlight business developer.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92571 Viewers
82 Video lessons · 104150 Viewers
71 Video lessons · 75677 Viewers
56 Video lessons · 107061 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.