To keep your excitement about macros going, I want to give you some ideas for moving forward with them, and show you a few more code snippets to test in your VSMacros module. Because you created a Try...Catch block, you could create macros for
If...Then...Else blocks
For...Next loops
Variable declarations
While Loops
Almost all classes I write use the System.Data.SqlClient namespace, and maybe the System.IO namespace or System.XML namespace. To add text to the top of your file, you can use the StartOfDocument method, as in Listing 16.7.
Sub StartOfDocumentExample() Dim txt As TextSelection = DTE.ActiveDocument.Selection With txt .StartOfDocument() .Text = "Imports System.Data.SqlClient" & vbCrLf .Text = "Imports System.IO" & vbCrLf .Text = "Imports System.XML" & vbCrLf End With End Sub
Sub StartOfDocumentExample() Dim txt As TextSelection = DTE.ActiveDocument.Selection With txt .StartOfDocument() .Text = "using System.Data.SqlClient;" & vbCrLf .Text = "using System.IO;" & vbCrLf .Text = "using System.XML;" & vbCrLf End With End Sub
When you run this macro, the correct code is added to the top of your class file.
You can add new task items to the Task List in Visual Studio .NET using the TaskItems object. The code in Listing 16.8 will prompt you for a task, and then add it to the Task List. This is a very cool macro, and it might give you more ideas about how to use the Task List.
Sub AddNewTask()
Dim win As Window = DTE.Windows.Item _
(Constants.vsWindowKindTaskList)
Dim TL As TaskList = win.Object
Dim TLItem As TaskItem
Dim strTask As String
strTask = InputBox("Please enter the Task")
If strTask.Trim.Length > 0 Then
' Add a task to the Task List.
TLItem = TL.TaskItems.Add(" ", " ", strTask, _
vsTaskPriority.vsTaskPriorityHigh, _
vsTaskIcon.vsTaskIconUser, True, , 10, , )
End If
End Sub
Notice that the code in Listing 16.8 also uses the InputBox function to prompt you for the description of the task. Using the InputBox function enables you to retrieve information from the user to use in your code.
This is also useful for prompting the user for iterations, variable scope, or other items that you might want to write macros for.
Creating a code library is a great idea to preserve all the great work you do everyday. Using macros, you can save the code you write to a SQL Server table.
The macro in Listing 16.9 copies the selected text in the Code Editor, prompts you for a description, and then inserts the text into a database. You must add a reference to the System.Data assembly in the References node of the Macros project for this to work. Then add the Imports System.Data and System.Data.SqlClient at the top of the VSMacros module.
Sub AddToCodeLibrary()
Dim code As String
Dim txt As TextSelection = DTE.ActiveDocument.Selection
code = txt.Text
Dim cn As SqlConnection = _
New SqlConnection _
("Integrated Security=SSPI;database=Utilities;server=jb1gs\netsdk")
cn.Open()
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.StoredProcedure
Dim desc As String
desc = InputBox("Enter a description for this code snippet")
With cmd
.Connection = cn
.CommandText = "insert_CodeSnippets"
.Parameters.Add("@code_1", _
SqlDbType.VarChar, 5000).Value = code
.Parameters.Add("@description_2", _
SqlDbType.VarChar, 100).Value = desc
.ExecuteNonQuery()
End With
MsgBox("Code added to the database")
cn.Close()
cn = Nothing
End Sub
I created a database named Utilities and a table named Code to hold all of my data. The SQL script for the table and stored procedure are given in Listing 16.10.
CREATE TABLE [dbo].[CodeSnippets] ( [ID] [int] IDENTITY (1, 1) NOT NULL , [Code] [varchar] (5000) COLLATE SQL_Latin1_General_ CP1_CI_AS NOT NULL , [Description] [varchar] (100) COLLATE SQL_Latin1 _General_CP1_CI_AS NOT NULL , [DateAdded] [datetime] NOT NULL ) ON [PRIMARY] GO CREATE PROCEDURE [insert_CodeSnippets] (@Code_1 [varchar](5000), @Description_2 [varchar](100)) AS INSERT INTO [Utilities].[dbo].[CodeSnippets] ( [Code], [Description]) VALUES ( @Code_1, @Description_2) GO
Now you can start saving your code to a SQL Server database and keep it forever. Tomorrow, you're going to learn how to grab the code you saved to the database and create a tab for it in the Toolboxthen the code will be there for you to drag into the code window when you need it.