@@ -12,32 +12,86 @@ public struct QualifiedModuleName
12
12
{
13
13
private static string GetDisplayName ( VBProject project )
14
14
{
15
- if ( ! string . IsNullOrEmpty ( Path . GetDirectoryName ( project . BuildFileName ) ) )
15
+ //Try reading the filename first
16
+ try
16
17
{
17
- return Path . GetFileName ( project . FileName ) ;
18
+ if ( ! string . IsNullOrEmpty ( Path . GetDirectoryName ( project . BuildFileName ) ) )
19
+ {
20
+ return Path . GetFileName ( project . FileName ) ;
21
+ }
22
+ }
23
+ catch
24
+ { //The GetFileName getter probably threw
25
+ }
26
+
27
+ if ( project . Protection == vbext_ProjectProtection . vbext_pp_none )
28
+ {
29
+ //Try reading the top-most document-type component's Properties("Name") value
30
+ //Eg. A Workbook's parent is the application, so read the workbook's name
31
+ try
32
+ {
33
+ var nameProperty = project . VBComponents . Cast < VBComponent > ( )
34
+ . FirstOrDefault ( comp => comp . Type == vbext_ComponentType . vbext_ct_Document
35
+ && comp . Properties . Item ( "Name" ) . Value != null
36
+ && comp . Properties . Item ( "Parent" )
37
+ . Object . Equals ( comp . Properties . Item ( "Application" ) . Object ) )
38
+ . Properties . Cast < Property > ( ) . FirstOrDefault ( property => property . Name == "Name" ) ;
39
+ return nameProperty == null
40
+ ? null
41
+ : nameProperty . Value . ToString ( ) ;
42
+ }
43
+ catch
44
+ {
45
+ //The Properties collection either wasn't available, or didn't have the expected properties
46
+ }
47
+
48
+ //Try reading the top-most document-type component's parent's Properties("Name") value
49
+ // Eg. A PowerPoint Slide is top level, but it's parent is a Presentation (that is NOT a vbComponent)
50
+ try
51
+ {
52
+ var parentProp = project . VBComponents . Cast < VBComponent > ( )
53
+ . FirstOrDefault ( comp => comp . Type == vbext_ComponentType . vbext_ct_Document
54
+ && comp . Properties . Item ( "Parent" ) . Value != null )
55
+ . Properties . Cast < Property > ( ) . FirstOrDefault ( property => property . Name == "Parent" ) ;
56
+
57
+ Properties props = null ;
58
+ Property nameProperty = null ;
59
+ if ( parentProp . Value is Properties )
60
+ {
61
+ props = ( Properties ) parentProp . Value ;
62
+ nameProperty = props . Cast < Property > ( ) . FirstOrDefault ( property => property . Name == "Name" ) ;
63
+ }
64
+
65
+ return nameProperty == null
66
+ ? null
67
+ : nameProperty . Value . ToString ( ) ;
68
+ }
69
+ catch
70
+ {
71
+ //The Properties collection either wasn't available, or didn't have the expected properties
72
+ }
18
73
}
19
-
20
- //Don't need to check protection, as a protected project is saved, by definition
21
- var firstOrDefault = project . VBComponents . Cast < VBComponent > ( )
22
- . Where ( component => component . Type == vbext_ComponentType . vbext_ct_Document
23
- && component . Properties . Count > 1 )
24
- . SelectMany ( component => component . Properties . OfType < Property > ( ) )
25
- . FirstOrDefault ( property => property . Name == "Name" ) ;
26
- return firstOrDefault == null
27
- ? null
28
- : firstOrDefault . Value . ToString ( ) ;
74
+ return null ;
29
75
}
30
76
31
77
private static string GetDisplayName ( VBComponent component )
32
78
{
33
- if ( component . Type ! = vbext_ComponentType . vbext_ct_Document )
79
+ if ( component . Type = = vbext_ComponentType . vbext_ct_Document )
34
80
{
35
- return null ;
81
+ //Check for a valid properties collection (some hosts don't validate the Properties method unless the component's designer is open in the host
82
+ try
83
+ {
84
+ var nameProperty = component . Properties . Item ( "Name" ) ;
85
+ return nameProperty == null
86
+ ? null
87
+ : nameProperty . Value . ToString ( ) ;
88
+ }
89
+ catch
90
+ {
91
+ //The component isn't open in the host, the Properties Collection is probably inaccessible
92
+ }
36
93
}
37
- var nameProperty = component . Properties . Cast < Property > ( ) . SingleOrDefault ( p => p . Name == "Name" ) ;
38
- return nameProperty == null
39
- ? null
40
- : nameProperty . Value . ToString ( ) ;
94
+ return null ;
41
95
}
42
96
43
97
public static string GetProjectId ( VBProject project )
0 commit comments