none
VB Runtime Error '5' in C# Program

    Question

  • I'm copying a some Worksheets from one excel file into another workbook.  The copying succeeds and is saved to a file, however, after the first copy, I get an error in a dialog box with header 'Microsoft Visual Basic' : "Run-time error '5':  Invalid procedure call or argument:  '"

    How is this possible?  My program is written in C#.

    Here's the code:
    Code Snippet

    object missing = System.Reflection.Missing.Value;

    /**Get Sheets.*/
    sheet = (Excel._Worksheet)workbook.Worksheets.get_Item(1);
    otherSheet = (Excel._Worksheet)otherWorkbook.Worksheets.get_Item(2);
    /**Insert the first sheet into the Other workbook after the otherSheet.*/
    sheet.Copy(missing,otherSheet);
                       
    /**Now get the second sheet*/
    sheet = (Excel._Worksheet)workbook.Worksheets.get_Item(2);
    /**Get the sheet that was just inserted.*/
    otherSheet = (Excel._Worksheet)otherWorkbook.Worksheets.get_Item(3);
    /**Insert after this sheet.*/
    sheet.Copy(missing,otherSheet); // Error occurs here.


    Is this error occurring because the Workbook isn't really being updated before I get the second 'otherSheet'?
    Thursday, April 19, 2007 10:02 PM

Answers

  • That sort of depends. This is 2002 or later? And are you opening all the Excel workbooks in question through your code?

     

    If the answer to both of these is YES, then look at the AutomationSecurity property of the Application object. You can set that to not enable macros of any workbooks you open using automation. This will not change the user's macro security settings in any other context.

    Friday, April 20, 2007 8:17 AM
    Moderator
  • Found the source of the second error.  I had references to two files that contained the same or slightly different type.  So I removed one reference.  Now it works.

    Tuesday, May 01, 2007 3:34 PM

All replies

  • Hello Developer,

     

    I'm sure that you have some VBA Macro - attached to your Worksheet or

    or another old VB - COM AddIn loaded within Excel.

     

    Can you check this ?

     

    Hope this helps,

    greets, Helmut

    Thursday, April 19, 2007 10:16 PM
    Answerer
  • Is there some way to just suppress any runtime errors from popping up a dialog.  I don't mind the error, I just prefer not to see the dialog popup.
    Friday, April 20, 2007 2:58 AM
  • That sort of depends. This is 2002 or later? And are you opening all the Excel workbooks in question through your code?

     

    If the answer to both of these is YES, then look at the AutomationSecurity property of the Application object. You can set that to not enable macros of any workbooks you open using automation. This will not change the user's macro security settings in any other context.

    Friday, April 20, 2007 8:17 AM
    Moderator
  • This is the code that I have:

    Code Snippet

    Excel.Application myExcelApplication = new Excel.Application();
    myExcelApplication.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;


    I still get the error

    Code Snippet

    "Cannot implicitly convert type 'Microsoft.Office.Core.MsoAutomationSecurity [...obj\Debug\Interop.Microsoft.Office.Core.dll]' to 'Microsoft.Office.Core.MsoAutomationSecurity []' '


    What am I supposed to do about this?

     Cindy Meister wrote:

    That sort of depends. This is 2002 or later? And are you opening all the Excel workbooks in question through your code?

    If the answer to both of these is YES, then look at the AutomationSecurity property of the Application object. You can set that to not enable macros of any workbooks you open using automation. This will not change the user's macro security settings in any other context.

    Friday, April 20, 2007 2:33 PM
  • You still haven't told us the version of Excel you're automating...
    Monday, April 23, 2007 8:17 AM
    Moderator
  • I'm using Excel 2003.

     Cindy Meister wrote:
    You still haven't told us the version of Excel you're automating...
    Monday, April 23, 2007 2:32 PM
  •  DeveloperSQL wrote:
    This is the code that I have:

    Code Snippet

    Excel.Application myExcelApplication = new Excel.Application();
    myExcelApplication.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;


    I still get the error

    Code Snippet

    "Cannot implicitly convert type 'Microsoft.Office.Core.MsoAutomationSecurity [...obj\Debug\Interop.Microsoft.Office.Core.dll]' to 'Microsoft.Office.Core.MsoAutomationSecurity []' '


    What am I supposed to do about this?

     Cindy Meister wrote:

    That sort of depends. This is 2002 or later? And are you opening all the Excel workbooks in question through your code?

    If the answer to both of these is YES, then look at the AutomationSecurity property of the Application object. You can set that to not enable macros of any workbooks you open using automation. This will not change the user's macro security settings in any other context.

     

    Well, when I test (VS 2005 WinForms app with Excel 2003) this works with no problem

     

                    xlApp.AutomationSecurity = office.MsoAutomationSecurity.msoAutomationSecurityForceDisable;

    (I have a using statement mapping "office" to Microsoft.Office.Core)

     

    If your Windows and Excel languages aren't US English, you might be having a problem with thread culture. But usually I'd expect a very different kind of error message (from COM, not the .NET framework).

    Tuesday, April 24, 2007 1:27 PM
    Moderator
  • The error seems to be complaining that myExcelApplication.AutomationSecurity is a Microsoft.Office.Core.MsoAutomationSecurity [] array and not a Microsoft.Office.Core.MsoAutomationSecurity object.  How could this be?

     Cindy Meister wrote:
    DeveloperSQL wrote:
    This is the code that I have:

    Code Snippet

    Excel.Application myExcelApplication = new Excel.Application();
    myExcelApplication.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;


    I still get the error

    Code Snippet

    "Cannot implicitly convert type 'Microsoft.Office.Core.MsoAutomationSecurity [...obj\Debug\Interop.Microsoft.Office.Core.dll]' to 'Microsoft.Office.Core.MsoAutomationSecurity []' '


    What am I supposed to do about this?

    Cindy Meister wrote:

    That sort of depends. This is 2002 or later? And are you opening all the Excel workbooks in question through your code?

    If the answer to both of these is YES, then look at the AutomationSecurity property of the Application object. You can set that to not enable macros of any workbooks you open using automation. This will not change the user's macro security settings in any other context.

    Well, when I test (VS 2005 WinForms app with Excel 2003) this works with no problem

    xlApp.AutomationSecurity = office.MsoAutomationSecurity.msoAutomationSecurityForceDisable;

    (I have a using statement mapping "office" to Microsoft.Office.Core)

    If your Windows and Excel languages aren't US English, you might be having a problem with thread culture. But usually I'd expect a very different kind of error message (from COM, not the .NET framework).

    Wednesday, April 25, 2007 5:44 PM
  • Hello,

     

    I re-checked works with Excel 2007 too.

    I recommend to go too Control-Panel Software and do a repair of Excel.

    Also download and reinstall the Office 2003 PIA's

     

    http://www.microsoft.com/downloads/details.aspx?familyid=3c9a983a-ac14-4125-8ba0-d36d67e0f4ad&displaylang=en

     

    If that won't help - I don't know.

    Greets, Helmut

    Wednesday, April 25, 2007 7:48 PM
    Answerer
  • Found the source of the second error.  I had references to two files that contained the same or slightly different type.  So I removed one reference.  Now it works.

    Tuesday, May 01, 2007 3:34 PM
  • I've had the same problem, it turns out that the problem was that my Microsoft.Office.Core was based on "Microsoft Office 12.0 object library" while everything else referred to version 11.0. Although the AutomationSecurity object is present in both versions, you really can't exchange objects between different versions of the office libraries.
    Monday, November 24, 2008 12:02 PM