Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
A Visual Studio macro is code that can be run by the Visual Studio IDE. A macro is usually used to automate some task in Visual Studio-- for instance, inserting Boilerplate comments, formatting or cleaning up portions of your code. Macros are written in a special version of Visual Basic. Plus, they are written and edited in a special version of Visual Studio. For today's demo, I'm in a solution called UsingMacros, and I've got the PixelSmithDesktop application open so that I can look at the source code. I'm going to double-click on MainWindow.xaml.cs.
So to work with macros, you need to go to the Tools menu and then the Macros section. There are a number of menu items in here. The one we're interested in right now is the Macro Explorer. We're going to enable the Macro Explorer by clicking on this menu item. The Macro Explorer lives over here with the Solution Explorer and Class View, and it shows that we currently have two areas of macros. The top one is my RecordingModule-- we'll look at that in a minute--and at the bottom we have this Samples module.
Microsoft includes a number of samples for you to look at and learn from their examples. So, what I can do is click in my code and then come over here to this VSEditor Samples section and look at things like this BeginningOfFunction method, which I would guess--if I put my cursor here on line 30 and ran the BeginningOfFunction function, Run--it moves my cursor to the first line of the function. I'll put a comment in here in C#, and then I'll choose InsertDate.
It put the current date in there. Now, if you want to learn how these macros work, you can right-click and choose Edit, which will then launch the special version of Visual Studio, and here's the code that Microsoft wrote to insert the date. A good tool, if you're brand-new to macros, is using the Macro Recorder. Recorded macros go in this area. Let me show you how that's done. I'll come over here to my code, and I'll turn on macro recording: Tools > Macros > Record TemporaryMacro.
It's hard to see, but Microsoft has added a special macro recording toolbar up here. When I'm done, I'm going to click on the Stop Recording. Now, everything that I do in my code is being recorded and written as Visual Basic code. So I'll put a comment in, and then I'll draw a line across the screen. When I'm finished writing the macro, I can click on Stop Recording.
Now, if you'll notice over here in MyMacros, there is a TemporaryMacro now. And I can right-click on that and go learn how to write comments and text inside my code. So, over here, I can see that I've got this DTE.ActiveDocument, and so on. DTE stands for Design-Time Environment, and ActiveDocument obviously is where my cursor was, and the Selection.Text is meaning I'm writing some text into that selected document.
Then I'm writing a blank line and then some more text and a blank line and so on. So this is a great tool for learning how to write the Visual Basic code, especially if it's not your native language. You can also bring in your own macros. I have a series of macros I use all the time in Visual Studio. To bring in your own macros, you right -click and choose Load Macro Project. For today's demo, we're going to be using a VSMACROS file that's included in the Exercise Files folder and then the Assets folder. So, I'm going to click on this and then click on Add. And then up here, you'll see that I have two macros.
We're going to look at the Search for Google macro. To show you how it works, I'm going to come down here and click on this BorderBrush property, and then I'm going to invoke my macro. As you can see, Visual Studio loaded a copy of Internet Explorer within the body of Visual Studio. It passed my string to the Google search engine. So you can see it's says search question mark q=BorderBrush. Let's look at the code. I'm going to right-click here. Choose Edit.
I'm also going to show you that you can put a breakpoint in your code and run this later. So here, I'm getting the current selected text--that was my word BorderBrush. I'm trimming off any excess space and storing it in the string. I'm checking the length of the string to make sure that something is selected in the designer. Then I'm encoding that string, using the UrlEncode, so that it's safe to pass to a domain, and then I'm going to concatenate that on the end of this string here which is passing the string to the Google search engine.
And then Navigate, that's telling Visual Studio to load the browser in the designer. Now, you notice I have a breakpoint in here. So I'm going to return to my code, I'll choose a different word, and then I'll right-click and choose Run. Notice that I switched immediately over to Visual Studio, even though I didn't have it running before. This is that special copy of Visual Studio. And at this point, I can step through my code by using F11, hover over the variables to find out what's contained in them, rewind my code by grabbing the yellow arrow.
All the tools that work in Visual Studio's normal Debugger work in this special copy. I'm going to go ahead and remove my breakpoint and then press F5. And again, you see my query string pass to Google. So, a macro is just some Visual Basic code that can be executed from within the Visual Studio IDE, and I use it to automate common tasks. Let me show you one more thing. I'd like to attach this macro to a keystroke. I'm going to click on this Close button, and then I'm going to go to my Tools menu and choose Options.
Next, I'm going to look in my Environment section and find the Keyboard section. The name of my macro is called SearchGoogle. So, I'm going to come over here and type in "goo". There is my macro, and now I'm going to come down here and press the desired keystroke that I would like to use to invoke this macro. I think Ctrl+Shift+G would be good. When I choose that, I see that that's already assigned to a pre-existing function inside Visual Studio. I have to decide if I want to erase this shortcut and replace it with my own, or pick a different keystroke.
I never use this keystroke, so I'm going to go ahead and reassign it to my Google macro. Then I'll click OK, and then I'll come down here, and we'll try this one here, Canvas.SetTop. So, I'll click here, and then I'll press Ctrl+Shift+G, and it worked beautifully. So, let's think about this. A macro is just some VB code that can be executed from within the Visual Studio IDE, and I use it to automate common tasks. For more complex scenarios, we need to turn to other tools, but that's a topic for the next movie.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96118 Viewers
56 Video lessons · 109955 Viewers
71 Video lessons · 78752 Viewers
131 Video lessons · 37776 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.