locked
C# more than one Excel.dll and PageSetup.AlignMarginsHeaderFooter RRS feed

  • Question

  • Hi, 

    In my vs 2008, I have more than one Excel.dll in the refrence.
    One is Interop.Excel 1.3.0.0, the other one is Microsoft.Office.Interop.Excel.dll 1.6.0.0.
    The Excel 12 has the PageSetup.AlignMarginsHeaderFooter, but the Excel 10 doesn't.
    In the VS 2008 C# code, I can use the PageSetup.AlignMarginsHeaderFooter.

    However, when the Excel automation comes to that code, it will have the error:
    "System.Runtime.InteropServices.COMException".
    How do I fix this problem?
    Thanks for help.

    Jason


    Monday, February 20, 2017 7:55 AM

Answers

  • Since you're using 2 different interop libraries you're going to be mixing types unless you're very careful. In theory you should be able to remove the 1.3 version of the interop assembly as the newer version should cover both. The problem is that your code is likely targeting 1.3 which doesn't have the type you're looking for and hence it blows up at runtime when it doesn't find it in COM. Switching to 1.6 should solve the problem at the cost that you can no longer target the earlier version of Excel.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Monday, February 20, 2017 4:04 PM
  • Hi JasonHuang8888,

    Thank you for posting here.

    According to your question is more related to Excel, I will move it to Excel for Developers forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by JasonHuang8888 Tuesday, February 21, 2017 2:57 AM
    Tuesday, February 21, 2017 2:42 AM

All replies

  • Since you're using 2 different interop libraries you're going to be mixing types unless you're very careful. In theory you should be able to remove the 1.3 version of the interop assembly as the newer version should cover both. The problem is that your code is likely targeting 1.3 which doesn't have the type you're looking for and hence it blows up at runtime when it doesn't find it in COM. Switching to 1.6 should solve the problem at the cost that you can no longer target the earlier version of Excel.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Monday, February 20, 2017 4:04 PM
  • Hi JasonHuang8888,

    Thank you for posting here.

    According to your question is more related to Excel, I will move it to Excel for Developers forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by JasonHuang8888 Tuesday, February 21, 2017 2:57 AM
    Tuesday, February 21, 2017 2:42 AM