/// <summary> /// Create a new VBA Project /// </summary> internal void Create() { if (Lcid > 0) { throw (new InvalidOperationException("Package already contains a VBAProject")); } ProjectID = "{5DD90D76-4904-47A2-AF0D-D69B4673604E}"; Name = "VBAProject"; SystemKind = eSyskind.Win32; //Default Lcid = 1033; //English - United States LcidInvoke = 1033; //English - United States CodePage = Encoding.Default.CodePage; MajorVersion = 1361024421; MinorVersion = 6; HelpContextID = 0; Modules.Add(new ExcelVBAModule(_wb.CodeNameChange) { Name = "ThisWorkbook", Code = "", Attributes = GetDocumentAttributes("ThisWorkbook", "0{00020819-0000-0000-C000-000000000046}"), Type = eModuleType.Document, HelpContext = 0 }); foreach (var sheet in _wb.Worksheets) { if (!Modules.Exists(sheet.Name)) { Modules.Add(new ExcelVBAModule(sheet.CodeNameChange) { Name = sheet.Name, Code = "", Attributes = GetDocumentAttributes(sheet.Name, "0{00020820-0000-0000-C000-000000000046}"), Type = eModuleType.Document, HelpContext = 0 }); } } _protection = new ExcelVbaProtection(this) { UserProtected = false, HostProtected = false, VbeProtected = false, VisibilityState = true }; }
private void ReadProjectProperties() { _protection = new ExcelVbaProtection(this); string prevPackage = ""; var lines = Regex.Split(ProjectStreamText, "\r\n"); foreach (string line in lines) { if (line.StartsWith("[")) { } else { var split = line.Split('='); if (split.Length > 1 && split[1].Length > 1 && split[1].StartsWith("\"")) //Remove any double qouates { split[1] = split[1].Substring(1, split[1].Length - 2); } switch (split[0]) { case "ID": ProjectID = split[1]; break; case "Document": string mn = split[1].Substring(0, split[1].IndexOf("/&H")); Modules[mn].Type = eModuleType.Document; break; case "Package": prevPackage = split[1]; break; case "BaseClass": Modules[split[1]].Type = eModuleType.Designer; Modules[split[1]].ClassID = prevPackage; break; case "Module": Modules[split[1]].Type = eModuleType.Module; break; case "Class": Modules[split[1]].Type = eModuleType.Class; break; case "HelpFile": case "Name": case "HelpContextID": case "Description": case "VersionCompatible32": break; //393222000" case "CMG": byte[] cmg = Decrypt(split[1]); _protection.UserProtected = (cmg[0] & 1) != 0; _protection.HostProtected = (cmg[0] & 2) != 0; _protection.VbeProtected = (cmg[0] & 4) != 0; break; case "DPB": byte[] dpb = Decrypt(split[1]); if (dpb.Length >= 28) { byte reserved = dpb[0]; var flags = new byte[3]; Array.Copy(dpb, 1, flags, 0, 3); var keyNoNulls = new byte[4]; _protection.PasswordKey = new byte[4]; Array.Copy(dpb, 4, keyNoNulls, 0, 4); var hashNoNulls = new byte[20]; _protection.PasswordHash = new byte[20]; Array.Copy(dpb, 8, hashNoNulls, 0, 20); //Handle 0x00 bitwise 2.4.4.3 for (int i = 0; i < 24; i++) { int bit = 128 >> (int)((i % 8)); if (i < 4) { if ((int)(flags[0] & bit) == 0) { _protection.PasswordKey[i] = 0; } else { _protection.PasswordKey[i] = keyNoNulls[i]; } } else { int flagIndex = (i - i % 8) / 8; if ((int)(flags[flagIndex] & bit) == 0) { _protection.PasswordHash[i - 4] = 0; } else { _protection.PasswordHash[i - 4] = hashNoNulls[i - 4]; } } } } break; case "GC": _protection.VisibilityState = Decrypt(split[1])[0] == 0xFF; break; } } } }