Skip to content

Commit d51741e

Browse files
committed
Fixes GetDisplayName
Adds extra method for projects that have documents without a document-component (eg. PowerPoint)
1 parent 8adf30b commit d51741e

File tree

1 file changed

+72
-18
lines changed

1 file changed

+72
-18
lines changed

Rubberduck.VBEEditor/QualifiedModuleName.cs

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,86 @@ public struct QualifiedModuleName
1212
{
1313
private static string GetDisplayName(VBProject project)
1414
{
15-
if (!string.IsNullOrEmpty(Path.GetDirectoryName(project.BuildFileName)))
15+
//Try reading the filename first
16+
try
1617
{
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+
}
1873
}
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;
2975
}
3076

3177
private static string GetDisplayName(VBComponent component)
3278
{
33-
if (component.Type != vbext_ComponentType.vbext_ct_Document)
79+
if (component.Type == vbext_ComponentType.vbext_ct_Document)
3480
{
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+
}
3693
}
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;
4195
}
4296

4397
public static string GetProjectId(VBProject project)

0 commit comments

Comments
 (0)