public static void Execute(ProjectItem projectItem) { string textToInsert = string.Format(Text, projectItem.Name); var objTextDoc = (TextDocument)projectItem.Document.Object("TextDocument"); EditPoint editPoint = objTextDoc.StartPoint.CreateEditPoint(); while (string.IsNullOrWhiteSpace(editPoint.GetLineText())) { editPoint.DeleteCurrentLine(); } if (projectItem.ReadAllText().TrimStart().StartsWith(textToInsert, StringComparison.InvariantCulture)) { return; } var txtSel = (TextSelection)projectItem.Document.Selection; txtSel.MoveTo(0, 0); TextRanges ranges = null; int findOptions = (int)(vsFindOptions.vsFindOptionsRegularExpression | vsFindOptions.vsFindOptionsFromStart); if (txtSel.FindPattern(RegExText, findOptions, ref ranges)) { var firstRange = ranges.OfType <TextRange>().FirstOrDefault(); if (firstRange != null) { var rangeText = firstRange.StartPoint.GetText(firstRange.EndPoint); if (rangeText.Trim().Equals(textToInsert.Trim(), StringComparison.InvariantCulture)) { return; } firstRange.StartPoint.Delete(firstRange.EndPoint); } } txtSel.GotoLine(1, true); txtSel.StartOfLine(); txtSel.NewLine(); txtSel.GotoLine(1, true); txtSel.Insert(textToInsert); }