public MockedCodeExplorer(ProjectType projectType, IReadOnlyList <ComponentType> componentTypes, IReadOnlyList <string> code = null) : this() { if (code != null && componentTypes.Count != code.Count) { Assert.Inconclusive("MockedCodeExplorer Setup Error"); } var builder = new MockVbeBuilder(); var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected, projectType); for (var index = 0; index < componentTypes.Count; index++) { var item = componentTypes[index]; if (item == ComponentType.UserForm) { project.MockUserFormBuilder($"{item.ToString()}{index}", code is null ? string.Empty : code[index]).AddFormToProjectBuilder(); } else { project.AddComponent($"{item.ToString()}{index}", item, code is null ? string.Empty : code[index]); } } VbComponents = project.MockVBComponents; VbComponent = project.MockComponents.First(); VbProject = project.Build(); Vbe = builder.AddProject(VbProject).Build(); SetupViewModelAndParse(); VbProject.SetupGet(m => m.VBComponents.Count).Returns(componentTypes.Count); }
private static void ReadExcelFile(string path) { using (var storage = new VbProject(path)) { PrintStorage(storage); } }
/// <summary> /// This function doesn't have anything to do specifically with this library, but is something that can be done /// simply with OpenXML /// </summary> private static void ReplaceVbaParts() { Console.WriteLine("Replace an existing vbProject of an Excel file with another vbProject"); Console.WriteLine(); Console.WriteLine("Enter path of workbook to open: "); string path = Console.ReadLine(); if (!File.Exists(path)) { throw new FileNotFoundException(String.Format("File {0} does not exist", path)); } Console.WriteLine("Enter path of .bin file to open"); string binPath = Console.ReadLine(); if (!File.Exists(binPath)) { throw new FileNotFoundException(String.Format("File {0} does not exist", binPath)); } using (SpreadsheetDocument wb = SpreadsheetDocument.Open(path, true)) { var wbPart = wb.WorkbookPart; using (var storage = new VbProject(wbPart)) { PrintStorage(storage); } Console.WriteLine(); Console.WriteLine("---------------REPLACING VBA PART----------------"); Console.WriteLine(); // Replace parts var vba = wbPart .GetPartsOfType <VbaProjectPart>() .SingleOrDefault(); if (vba != null) { wbPart.DeletePart(vba); } VbaProjectPart newVbaPart = wbPart.AddNewPart <VbaProjectPart>(); using (var stream = File.OpenRead(binPath)) { newVbaPart.FeedData(stream); } using (var storage = new VbProject(wbPart)) { PrintStorage(storage); } wbPart.Workbook.Save(); } }
public static void AddProjectFromCode(this IVBE vbe, string moduleName, string inputCode) { var project = new VbProject(vbe, "TestProject1", "", ProjectProtection.Unprotected); ComponentType componentType = inputCode.GetModuleType(); string cleanedCodeContent = inputCode.StripVbAttributes(); project.AddComponent(moduleName, componentType, cleanedCodeContent); vbe.AddProject(project); }
private static void PrintStorage(VbProject VbProject) { PrintStorage(VbProject.AsVbaStorage()); }