public void Dispose() { PowerPointDocument?.Close(); PowerPointApp?.Quit(); if (PowerPointApp != null) { while (Marshal.ReleaseComObject(PowerPointApp) != 0) { } } if (PowerPointDocument != null) { while (Marshal.ReleaseComObject(PowerPointDocument) != 0) { } } PowerPointApp = null; PowerPointDocument = null; GC.Collect(); GC.WaitForPendingFinalizers(); }
/// <summary> /// Performs basic validation on the file being analyzed by this <see cref="FileAnalyzer" />. /// </summary> /// <returns>A <see cref="FileValidationResult" /> object representing the result of validation.</returns> public override FileValidationResult Validate() { try { using (PowerPointApp app = new PowerPointApp()) { app.Open(File); } return(FileValidationResult.Pass); } catch (COMException ex) { LogWarn("File failed to open: " + ex.Message); return(FileValidationResult.Fail(ex.Message)); } }
/// <summary> /// Gets information about a document, such as page count, title, and author. /// </summary> /// <returns>A <see cref="DocumentProperties" /> object.</returns> public override DocumentProperties GetProperties() { DocumentProperties properties = new DocumentProperties(File); try { using (PowerPointApp app = new PowerPointApp()) { Presentation presentation = app.Open(File); properties.Pages = (short)presentation.Slides.Count; // Get the page orientation switch (presentation.PageSetup.SlideOrientation) { case MsoOrientation.msoOrientationVertical: properties.Orientation = Orientation.Portrait; break; case MsoOrientation.msoOrientationHorizontal: properties.Orientation = Orientation.Landscape; break; case MsoOrientation.msoOrientationMixed: properties.Orientation = Orientation.Mixed; break; } // Retrieve built-in document properties properties.Title = presentation.BuiltInDocumentProperties.Item["Title"].Value; properties.Author = presentation.BuiltInDocumentProperties.Item["Author"].Value; properties.Application = presentation.BuiltInDocumentProperties.Item["Application Name"].Value; } } catch (COMException ex) { // Log the error, then return whatever properties have been collected. LogWarn("Failure retrieving properties: " + ex.Message); } return(properties); }
/// <summary> Returns the type of Office Application that is hosting the VBE. </summary> public static IHostApplication HostApplication(this IVBE vbe) { var host = Path.GetFileName(System.Windows.Forms.Application.ExecutablePath).ToUpperInvariant(); //This needs the VBE as a ctor argument. if (host.Equals("SLDWORKS.EXE")) { return(new SolidWorksApp(vbe)); } //The rest don't. if (HostAppMap.ContainsKey(host)) { return((IHostApplication)Activator.CreateInstance(HostAppMap[host])); } //Guessing the above will work like 99.9999% of the time for supported applications. var project = vbe.ActiveVBProject; { if (project.IsWrappingNullReference) { const int ctlViewHost = 106; var commandBars = vbe.CommandBars; var hostAppControl = commandBars.FindControl(ControlType.Button, ctlViewHost); { IHostApplication result; if (hostAppControl.IsWrappingNullReference) { result = null; } else { switch (hostAppControl.Caption) { case "Microsoft Excel": result = new ExcelApp(); break; case "Microsoft Access": result = new AccessApp(); break; case "Microsoft Word": result = new WordApp(); break; case "Microsoft PowerPoint": result = new PowerPointApp(vbe); break; case "Microsoft Outlook": result = new OutlookApp(); break; case "Microsoft Project": result = new ProjectApp(); break; case "Microsoft Publisher": result = new PublisherApp(); break; case "Microsoft Visio": result = new VisioApp(); break; case "AutoCAD": result = new AutoCADApp(); break; case "CorelDRAW": result = new CorelDRAWApp(); break; case "SolidWorks": result = new SolidWorksApp(vbe); break; default: result = null; break; } } return(result); } } var references = project.References; { foreach (var reference in references.Where(reference => (reference.IsBuiltIn && reference.Name != "VBA") || (reference.Name == "AutoCAD"))) { switch (reference.Name) { case "Excel": return(new ExcelApp(vbe)); case "Access": return(new AccessApp()); case "Word": return(new WordApp(vbe)); case "PowerPoint": return(new PowerPointApp(vbe)); case "Outlook": return(new OutlookApp()); case "MSProject": return(new ProjectApp()); case "Publisher": return(new PublisherApp()); case "Visio": return(new VisioApp()); case "AutoCAD": return(new AutoCADApp()); case "CorelDRAW": return(new CorelDRAWApp(vbe)); case "SolidWorks": return(new SolidWorksApp(vbe)); } } } } return(null); }