I derived a small macro script from Visual Studio Macro to Format all Files in a Solution but unfortunately it doesn't work with xml, xaml, config etc. All ProjectItem
that are xml-based normally throw an exception (command not available) when they were opened in their primary view vsViewKindPrimary
:
我从Visual Studio宏派生了一个小宏脚本,以格式化解决方案中的所有文件,但不幸的是,它与xml、xaml、config等都不兼容。
Dim projectItem As ProjectItem ' actually this is a parameter of a sub'
Dim window As Window = projectItem.Open(Constants.vsViewKindPrimary)
window.Activate()
projectItem.Document.DTE.ExecuteCommand("Edit.FormatDocument")
window.Close(vsSaveChanges.vsSaveChangesYes) ' actually this is part of a finally block'
Results in:
结果:
System.Runtime.InteropServices.COMException (0x80004005): Command "Edit.FormatDocument" is not available.
at EnvDTE.DTEClass.ExecuteCommand(String CommandName, String CommandArgs)
When opening them as text with vsViewKindTextView
they stay as they are, although the Edit.FormatDocument
could be executed.
当以vsViewKindTextView作为文本打开时,它们会保持原样,尽管编辑。FormatDocument可以执行。
Is there another command that must be used for xml files? Is there something wrong with the code?
是否有另一个命令必须用于xml文件?代码有问题吗?
1 个解决方案
#1
0
I'm seeing the same issue, but have a slightly different repro. This code works for .cpp files, but not .xml files:
我看到了同样的问题,但有一个稍微不同的repro。此代码适用于.cpp文件,但不是。xml文件:
EnvDTE.Solution soln = System.Activator.CreateInstance(
Type.GetTypeFromProgID("VisualStudio.Solution.11.0")) as EnvDTE.Solution;
soln.DTE.ItemOperations.OpenFile(file);
soln.DTE.ExecuteCommand("Edit.FormatDocument");
If I replace the last line with the following, it leaves the file unchanged:
如果我用下面的方法替换最后一行,它将保持文件不变:
TextSelection selection = soln.DTE.ActiveDocument.Selection as TextSelection;
selection.SelectAll();
selection.SmartFormat();
I've tried for a while but can't find a work-around.
我已经试了一段时间了,但还是找不到工作。
#1
0
I'm seeing the same issue, but have a slightly different repro. This code works for .cpp files, but not .xml files:
我看到了同样的问题,但有一个稍微不同的repro。此代码适用于.cpp文件,但不是。xml文件:
EnvDTE.Solution soln = System.Activator.CreateInstance(
Type.GetTypeFromProgID("VisualStudio.Solution.11.0")) as EnvDTE.Solution;
soln.DTE.ItemOperations.OpenFile(file);
soln.DTE.ExecuteCommand("Edit.FormatDocument");
If I replace the last line with the following, it leaves the file unchanged:
如果我用下面的方法替换最后一行,它将保持文件不变:
TextSelection selection = soln.DTE.ActiveDocument.Selection as TextSelection;
selection.SelectAll();
selection.SmartFormat();
I've tried for a while but can't find a work-around.
我已经试了一段时间了,但还是找不到工作。