Overview:
Microsoft Visual Studio is a tool in and of itself. Pun intended. I am trying to use EnvDTE from a System.Windows.Window that is being used as a plugin for Visual Studio. I do not want to access the project from which the plugin was created, rather the currently open project at the time the window is in use (whole different question). Anyway, I am running into an issue with the EnvDTE.EditPoint.Insert() method where I am getting the exception 'COM Exception was caught 'Exception from HRESULT: 0x80041001'' whenever I attempt to use it.
Here is my code:
public class DTEHelper
{
static private DTE _dte = null;
static private AddIn _addIn = null;
static private ProjectItem _pItem = null;
static private TextDocument _tDoc = null;
static private EditPoint _ePoint = null;
static private Project _projP = null;
static private SolutionBuild _sBuild = null;
public DTE dte { get { return _dte; } set { _dte = value; } }
public AddIn addIn { get { return _addIn; } set { _addIn = value; } }
public ProjectItem pItem { get { return _pItem; } set { _pItem = value; } }
public TextDocument tDoc { get { return _tDoc; } set { _tDoc = value; } }
public EditPoint ePoint { get { return _ePoint; } set { _ePoint = value; } }
public Project projP { get { return _projP; } set { _projP = value; } }
public SolutionBuild sBuild { get { return _sBuild; } set { _sBuild = value; } }
}
Problem Method:
private void addMethod(string file, DTEHelper dteHelper)
{
try
{
//Open existing file
System.Array projectFiles = (System.Array)dteHelper.dte.ActiveSolutionProjects;
EnvDTE.Project project = null;
if (projectFiles.Length > 0)
{
project = (EnvDTE.Project)(projectFiles.GetValue(0));
dteHelper.pItem = (from ProjectItem csFile in project.ProjectItems where csFile.Name.Contains(file) select csFile).First();
if (dteHelper.pItem.Name.Contains(".cs"))
{
EnvDTE.Window window = dteHelper.pItem.Open(Constants.vsViewKindCode);
TextDocument document = (TextDocument)window.Document.Object("TextDocument");
EditPoint edit = (EditPoint)document.CreateEditPoint();
edit.EndOfDocument();
edit.LineUp();
edit.Insert(""); //Problem Right Here
string fileName = dteHelper.pItem.get_FileNames(0);
dteHelper.pItem.SaveAs(fileName);
}
}
}
catch (Exception e)
{
MessageBox.Show(string.Format(System.Globalization.CultureInfo.CurrentUICulture, "Failed to add code to: " + file, this.ToString()),
"ERROR");
}
}
dteHelper.dte initialized when button is pressed in Window
dteHelper.dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
Turns out that everything is working fine, the only issue was that the file I was searching for ('foo.cs') was not 'in' the project (not recognized by the compiler) so I had to add it to the solution.
User contributions licensed under CC BY-SA 3.0