2016-03-29 5 views
2

In meiner Anwendung muss ich überprüfen, ob ein Excel-Dokument vb-Makros enthält. Also habe ich die folgende Methode geschrieben, um das Excel-Dokument zu überprüfen:Mit Interop.Excel überprüfen, ob die Excel-Datei VBA-Makros enthält

internal static bool ExcelContainsMacros(string pathToExcelFile) 
{ 
    bool hasMacros = true; 
    Microsoft.Office.Interop.Excel._Application excelApplication = new Microsoft.Office.Interop.Excel.Application(); 
    Microsoft.Office.Interop.Excel.Workbook workbooks = null; 
    try 
    {  
     object isReadonly = true; 
     workbooks = excelApplication.Workbooks.Open(
      pathToExcelFile, missing, isReadonly, missing, missing, missing, 
      missing, missing, missing, missing, missing, missing, 
      missing, missing, missing); 
     hasMacros = workbooks.HasVBProject;  
     LogHasMacros(hasMacros); 
    } 
    catch (Exception exception) 
    { 
     LogError(exception); 
    } 
    finally 
    { 
     excelApplication.Workbooks.Close(); 
     excelApplication.Quit(); 
    } 
    return hasMacros; 
} 

Bei einigen Excel-Dateien, die ich eine Nachricht von Excel erhalten mit einem Laufzeitfehler 91.

91: Objektvariable oder Mit Blockvariable nicht gesetzt

Ich debuggte und erkannte, dass die Nachricht bei dem Anruf an excelApplication.Workbooks.Close(); erscheint. Wenn ich diese Codezeile entferne, erscheint dieselbe Excel-Nachricht beim Aufruf von excelApplication.Quit();.

Was muss ich tun, um die Excel-Tabelle richtig zu schließen und verhindern, zeigt diese Meldung übertreffen?

+0

müssen Sie die Arbeitsmappe schließen, an der Sie gerade arbeiten. workbooks.Close() - auch Arbeitsmappen ist ein schrecklicher Name für Ihre Variable, da sie dem Arbeitsmappenobjekt sehr ähnlich ist. – Sorceri

+0

Sie können nur die fehlerhafte Zeile entfernen excelApplication.Workbooks.Close(): Es sollte funktionieren. –

+0

Wenn ich die "excelApplication.Quit();" lösche, hat dies keine Auswirkungen auf das Excel-Blatt? – Tomtom

Antwort

3

Einschlägige auf Ihre Aufgabe, Sie auf den folgenden raffinierten Code-Schnipsel beziehen, die .NET/C# verwendet, Microsoft.Office.Interop.Excel Objektbibliothek und Runtime.InteropServices.Marshal Objekt:

internal static bool? ExcelContainsMacros(string pathToExcelFile) 
{ 
    bool? _hasMacro = null; 
    Microsoft.Office.Interop.Excel._Application _appExcel = 
     new Microsoft.Office.Interop.Excel.Application(); 
    Microsoft.Office.Interop.Excel.Workbook _workbook = null; 
    try 
    { 
     _workbook = _appExcel.Workbooks.Open(pathToExcelFile, Type.Missing, true); 
     _hasMacro = _workbook.HasVBProject; 

     // close Excel workbook and quit Excel app 
     _workbook.Close(false, Type.Missing, Type.Missing); 
     _appExcel.Application.Quit(); // optional 
     _appExcel.Quit(); 

     // release COM object from memory 
     System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_appExcel); 
     _appExcel = null; 

     // optional: this Log function should be defined somewhere in your code   
     LogHasMacros(hasMacros); 
     return _hasMacro; 
    } 
    catch (Exception ex) 
    { 
     // optional: this Log function should be defined somewhere in your code   
     LogError(ex); 
     return null; 
    } 
    finally 
    { 
     if (_appExcel != null) 
     { 
      _appExcel.Quit(); 
      // release COM object from memory 
      System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_appExcel); 
     } 
    } 

Hinweis nullablebool? Art: in diesem Zusammenhang , null zurückgegeben von der Funktion zeigt den Fehler (mit anderen Worten, das Ergebnis ist unbestimmt), true/false Werte zeigen Anwesenheit/Abwesenheit von jedem VBA-Makro in Excel-Datei im Test.

Hoffe, das kann helfen.