private static string ExportToTempFile(this VBComponent component) { var path = Path.Combine(Path.GetTempPath(), component.Name + component.Type.FileExtension()); component.Export(path); return(path); }
/// <summary> /// Exports the component to the directoryPath. The file is name matches the component name and file extension is based on the component's type. /// </summary> /// <param name="component">The component to be exported to the file system.</param> /// <param name="directoryPath">Destination Path for the resulting source file.</param> public static void ExportAsSourceFile(this VBComponent component, string directoryPath) { string filePath = Path.Combine(directoryPath, component.Name + component.Type.FileExtension()); if (component.Type == vbext_ComponentType.vbext_ct_Document) { int lineCount = component.CodeModule.CountOfLines; if (lineCount > 0) { var text = component.CodeModule.get_Lines(1, lineCount); File.WriteAllText(filePath, text); } } else { component.Export(filePath); } }
/// <summary> /// Exports the component to the directoryPath. The file is name matches the component name and file extension is based on the component's type. /// </summary> /// <param name="component">The component to be exported to the file system.</param> /// <param name="directoryPath">Destination Path for the resulting source file.</param> public static string ExportAsSourceFile(this VBComponent component, string directoryPath) { var path = Path.Combine(directoryPath, component.Name + component.Type.FileExtension()); switch (component.Type) { case vbext_ComponentType.vbext_ct_MSForm: ExportUserFormModule(component, path); break; case vbext_ComponentType.vbext_ct_Document: ExportDocumentModule(component, path); break; default: component.Export(path); break; } return(path); }
public void Extract(string fileName, string exportPath) { int excelProcessID = 0; try { excel = new Microsoft.Office.Interop.Excel.Application(); excelProcessID = Process.GetProcessesByName("Excel").OrderByDescending(p => p.Id).Select(p => p.Id) .ToArray()[0]; workbooks = excel.Workbooks; workbook = workbooks.Open(fileName, false, true, Type.Missing, Type.Missing, Type.Missing, true, Type.Missing, Type.Missing, false, false, Type.Missing, false, true, Type.Missing); project = workbook.VBProject; string projectName = project.Name; var procedureType = Microsoft.Vbe.Interop.vbext_ProcKind.vbext_pk_Proc; VBComponents components = project.VBComponents; foreach (VBComponent component in components) { VBComponent vbComponent = (VBComponent)component; Properties props = vbComponent.Properties; vbComponent.Export($@"C:\Users\mvaysman\Source\Repos\vbaExtensions\{vbComponent.Name}.TXT"); } } catch (Exception e) { Console.WriteLine("Failed to export VBA Modules"); throw; } finally { excel.Quit(); Process.GetProcessById(excelProcessID).Kill(); } }
public void ExtractVBComponent(VBComponent comp, string path, string fName, bool overwrite) { // do we need to export component? if (comp.CodeModule.CountOfLines == 0) { return; } // define file name var extension = GetFileExtensionFor(comp); if (fName.Trim() == String.Empty) { fName = comp.Name + extension; } else if (fName.IndexOf("."[0]) == 0) { fName = fName + extension; } // define folder path if (path.EndsWith(@"\", StringComparison.CurrentCultureIgnoreCase)) { fName = path + fName; } else { fName = path + @"\" + fName; } // is it possible to write to path FileInfo file = new FileInfo(fName); if (file.Exists) { if (overwrite) { file.Delete(); } } comp.Export(fName); }