File tree Expand file tree Collapse file tree 1 file changed +25
-1
lines changed
Rubberduck.VBEEditor/VBEHost Expand file tree Collapse file tree 1 file changed +25
-1
lines changed Original file line number Diff line number Diff line change 1
1
using Microsoft . Vbe . Interop ;
2
+ using Microsoft . Office . Interop . Word ;
3
+ using System . Linq ;
2
4
3
5
namespace Rubberduck . VBEditor . VBEHost
4
6
{
@@ -10,13 +12,35 @@ public WordApp(VBE vbe) : base(vbe, "Word") { }
10
12
public override void Run ( QualifiedMemberName qualifiedMemberName )
11
13
{
12
14
var call = GenerateMethodCall ( qualifiedMemberName ) ;
13
- Application . Run ( call ) ;
15
+
16
+ ActivateProjectDocument ( qualifiedMemberName ) ;
17
+ //Prevent TE hanging should Application.Run fail
18
+ try
19
+ {
20
+ Application . Run ( call ) ;
21
+ }
22
+ //TODO - Let TestEngine know that the method failed
23
+ catch { } ;
14
24
}
15
25
16
26
protected virtual string GenerateMethodCall ( QualifiedMemberName qualifiedMemberName )
17
27
{
18
28
var moduleName = qualifiedMemberName . QualifiedModuleName . Component . Name ;
19
29
return string . Concat ( moduleName , "." , qualifiedMemberName . MemberName ) ;
20
30
}
31
+
32
+ protected virtual void ActivateProjectDocument ( QualifiedMemberName qualifiedMemberName )
33
+ {
34
+ // Word requires that the document be active for Application.Run to find the target Method in scope.
35
+ // Check the project's document or a document referring to a project's template is active.
36
+ var activeDoc = Application . ActiveDocument ;
37
+ var template = activeDoc . get_AttachedTemplate ( ) ;
38
+ var targetDoc = Application . Documents . Cast < Document > ( )
39
+ . FirstOrDefault ( doc => doc . Name == qualifiedMemberName . QualifiedModuleName . ProjectDisplayName
40
+ || doc . get_AttachedTemplate ( ) . Name == qualifiedMemberName . QualifiedModuleName . ProjectDisplayName ) ;
41
+ if ( activeDoc != targetDoc ) {
42
+ targetDoc . Activate ( ) ;
43
+ }
44
+ }
21
45
}
22
46
}
You can’t perform that action at this time.
0 commit comments