Skip to content

Commit f891c71

Browse files
committed
Add reflection based host discovery (should be much more robust than caption scanning).
1 parent 45552df commit f891c71

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Rubberduck.VBEEditor/Extensions/IDEExtensions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,43 @@
55
using Rubberduck.VBEditor.Application;
66
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
77
using Rubberduck.VBEditor.SafeComWrappers.MSForms;
8+
using Exception = System.Exception;
89

910
namespace Rubberduck.VBEditor.Extensions
1011
{
1112
public static class VBEExtensions
1213
{
14+
private static readonly Dictionary<string, Type> HostAppMap = new Dictionary<string, Type>
15+
{
16+
{"EXCEL.EXE", typeof(ExcelApp)},
17+
{"WINWORD.EXE", typeof(WordApp)},
18+
{"MSACCESS.EXE", typeof(AccessApp)},
19+
{"POWERPNT.EXE", typeof(PowerPointApp)},
20+
{"OUTLOOK.EXE", typeof(OutlookApp)},
21+
{"WINPROJ.EXE", typeof(ProjectApp)},
22+
{"MSPUB.EXE", typeof(PublisherApp)},
23+
{"VISIO.EXE", typeof(VisioApp)},
24+
{"ACAD.EXE", typeof(AutoCADApp)},
25+
{"CORELDRW.EXE", typeof(CorelDRAWApp)},
26+
{"SLDWORKS.EXE", typeof(SolidWorksApp)},
27+
};
28+
1329
/// <summary> Returns the type of Office Application that is hosting the VBE. </summary>
1430
public static IHostApplication HostApplication(this IVBE vbe)
1531
{
32+
var host = Path.GetFileName(System.Windows.Forms.Application.ExecutablePath).ToUpperInvariant();
33+
//This needs the VBE as a ctor argument.
34+
if (host.Equals("SLDWORKS.EXE"))
35+
{
36+
return new SolidWorksApp(vbe);
37+
}
38+
//The rest don't.
39+
if (HostAppMap.ContainsKey(host))
40+
{
41+
return (IHostApplication)Activator.CreateInstance(HostAppMap[host]);
42+
}
43+
44+
//Guessing the above will work like 99.9999% of the time for supported applications.
1645
var project = vbe.ActiveVBProject;
1746
{
1847
if (project.IsWrappingNullReference)
@@ -114,6 +143,10 @@ public static IHostApplication HostApplication(this IVBE vbe)
114143
/// <summary> Returns whether the host supports unit tests.</summary>
115144
public static bool HostSupportsUnitTests(this IVBE vbe)
116145
{
146+
var host = Path.GetFileName(System.Windows.Forms.Application.ExecutablePath).ToUpperInvariant();
147+
if (HostAppMap.ContainsKey(host)) return true;
148+
//Guessing the above will work like 99.9999% of the time for supported applications.
149+
117150
var project = vbe.ActiveVBProject;
118151
{
119152
if (project.IsWrappingNullReference)

0 commit comments

Comments
 (0)