Skip to content

Commit a4012d9

Browse files
committed
Prevent crash when loading non-existent userform.
1 parent 18547ca commit a4012d9

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Rubberduck.VBEEditor/Extensions/VBComponentsExtensions.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,17 @@ public static void ImportSourceFile(this VBComponents components, string filePat
5656
}
5757
else if (ext == VBComponentExtensions.FormExtension)
5858
{
59-
var component = components.Item(name);
60-
// note: vbeCode contains an extraneous line here:
61-
//var vbeCode = component.CodeModule.Lines().Split(new []{Environment.NewLine}, StringSplitOptions.None);
59+
VBComponent component;
60+
try
61+
{
62+
component = components.Item(name);
63+
}
64+
catch (IndexOutOfRangeException)
65+
{
66+
component = components.Add(vbext_ComponentType.vbext_ct_MSForm);
67+
component.Properties.Item("Caption").Value = name;
68+
component.Name = name;
69+
}
6270

6371
var nonAttributeLines = codeLines.TakeWhile(line => !line.StartsWith("Attribute")).Count();
6472
var attributeLines = codeLines.Skip(nonAttributeLines).TakeWhile(line => line.StartsWith("Attribute")).Count();

0 commit comments

Comments
 (0)