Skip to content

Commit dec0325

Browse files
committed
CorelDraw Works, including testing. HostApplication resolves more reliably for Excel and Word.
Added a ctor override to HostApplication Base, that gets the host application via vbe.vbProject.vbComponent.Properties. That allows the vbe to reliably find the real host, if there is more than 1 copy of a host application open.
1 parent 2ef226d commit dec0325

File tree

6 files changed

+51
-15
lines changed

6 files changed

+51
-15
lines changed

RetailCoder.VBE/UnitTesting/ProjectTestExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public static void RunMethodsWithAttribute<TAttribute>(this VBComponent componen
3636

3737
public static IEnumerable<TestMethod> TestMethods(this VBProject project)
3838
{
39-
var hostApp = project.VBE.HostApplication();
4039

4140
var result = project.VBComponents
4241
.Cast<VBComponent>()
@@ -50,7 +49,6 @@ public static IEnumerable<TestMethod> TestMethods(this VBProject project)
5049

5150
public static IEnumerable<TestMethod> TestMethods(this VBComponent component)
5251
{
53-
var hostApp = component.VBE.HostApplication();
5452

5553
if (component.Type == vbext_ComponentType.vbext_ct_StdModule
5654
&& component.CodeModule.HasAttribute<TestModuleAttribute>())

Rubberduck.VBEEditor/Extensions/VbeExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ public static IHostApplication HostApplication(this VBE vbe)
6262
switch (reference.Name)
6363
{
6464
case "Excel":
65-
return new ExcelApp();
65+
return new ExcelApp(vbe);
6666
case "Access":
6767
return new AccessApp();
6868
case "Word":
69-
return new WordApp();
69+
return new WordApp(vbe);
7070
case "PowerPoint":
7171
return new PowerPointApp();
7272
case "Outlook":
@@ -76,7 +76,7 @@ public static IHostApplication HostApplication(this VBE vbe)
7676
case "AutoCAD":
7777
return new AutoCADApp();
7878
case "CorelDRAW":
79-
return new CorelDRAWApp();
79+
return new CorelDRAWApp(vbe);
8080
}
8181
}
8282

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
using Corel.GraphicsSuite.Interop.CorelDRAW;
2+
using Microsoft.Vbe.Interop;
23

34
namespace Rubberduck.VBEditor.VBEHost
45
{
5-
public class CorelDRAWApp : HostApplicationBase<Application>
6+
public class CorelDRAWApp : HostApplicationBase<Corel.GraphicsSuite.Interop.CorelDRAW.Application>
67
{
78
public CorelDRAWApp() : base("CorelDRAW") { }
9+
public CorelDRAWApp(VBE vbe) : base(vbe, "CorelDRAW") { }
810

911
//Function RunMacro(ModuleName As String, MacroName As String, Parameter() As Variant) As Variant
1012
//Where ModuleName appears to mean ProjectName, and MacroName has to be provided as "ModuleName.ProcName"
13+
14+
//TODO:RunMacro can only execute methods in stand-alone projects (not document hosted projects)
15+
//TODO:Can only get a CorelDraw application if at least one document is open in CorelDraw.
1116

1217
public override void Run(QualifiedMemberName qualifiedMemberName)
1318
{
1419
var projectName = qualifiedMemberName.QualifiedModuleName.ProjectName;
1520
var memberName = qualifiedMemberName.QualifiedModuleName.ComponentName + "." + qualifiedMemberName.MemberName;
1621

17-
RunHelper(projectName, memberName);
18-
}
19-
20-
private void RunHelper(string ProjectName, string MemberName, params object[] p)
21-
{
22-
//Application.GMSManager.RunMacro(ProjectName, MemberName, p);
23-
//object GMS = Application.GMSManager;
22+
if (Application != null)
23+
{
24+
Application.GMSManager.RunMacro(projectName, memberName, new object[] {});
25+
}
2426
}
2527
}
2628
}

Rubberduck.VBEEditor/VBEHost/ExcelApp.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System.Reflection;
22
using Microsoft.Office.Interop.Excel;
3+
using Microsoft.Vbe.Interop;
34

45
namespace Rubberduck.VBEditor.VBEHost
56
{
6-
public class ExcelApp : HostApplicationBase<Application>
7+
public class ExcelApp : HostApplicationBase<Microsoft.Office.Interop.Excel.Application>
78
{
89
public ExcelApp() : base("Excel") { }
10+
public ExcelApp(VBE vbe) : base(vbe, "Excel") { }
911

1012
public override void Run(QualifiedMemberName qualifiedMemberName)
1113
{

Rubberduck.VBEEditor/VBEHost/HostApplicationBase.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Diagnostics;
33
using System.Runtime.InteropServices;
4+
using Microsoft.Vbe.Interop;
5+
using System.Linq;
46

57
namespace Rubberduck.VBEditor.VBEHost
68
{
@@ -24,6 +26,36 @@ protected HostApplicationBase(string applicationName)
2426
}
2527
}
2628

29+
protected HostApplicationBase(VBE vbe, string applicationName)
30+
{
31+
_applicationName = applicationName;
32+
33+
try
34+
{
35+
var appProperty = vbe.VBProjects
36+
.Cast<VBProject>()
37+
.Where(project => project.Protection == vbext_ProjectProtection.vbext_pp_none)
38+
.SelectMany(project => project.VBComponents.Cast<VBComponent>())
39+
.Where(component => component.Type == vbext_ComponentType.vbext_ct_Document
40+
&& component.Properties.Count > 1)
41+
.SelectMany(component => component.Properties.OfType<Property>())
42+
.FirstOrDefault(property => property.Name == "Application");
43+
if (appProperty != null)
44+
{
45+
Application = (TApplication)appProperty.Object;
46+
}
47+
else
48+
{
49+
Application = (TApplication)Marshal.GetActiveObject(applicationName + ".Application");
50+
}
51+
52+
}
53+
catch (COMException)
54+
{
55+
Application = null; // unit tests don't need it anyway.
56+
}
57+
}
58+
2759
~HostApplicationBase()
2860
{
2961
Dispose(false);

Rubberduck.VBEEditor/VBEHost/WordApp.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using Microsoft.Office.Interop.Word;
2+
using Microsoft.Vbe.Interop;
23

34
namespace Rubberduck.VBEditor.VBEHost
45
{
5-
public class WordApp : HostApplicationBase<Application>
6+
public class WordApp : HostApplicationBase<Microsoft.Office.Interop.Word.Application>
67
{
78
public WordApp() : base("Word") { }
9+
public WordApp(VBE vbe) : base(vbe, "Word") { }
810

911
public override void Run(QualifiedMemberName qualifiedMemberName)
1012
{

0 commit comments

Comments
 (0)