How to migrate VSTO for Excel 2007 and .NET 3.5 to .NET 4.0?

Answered How to migrate VSTO for Excel 2007 and .NET 3.5 to .NET 4.0?

  • Thursday, September 02, 2010 9:06 PM
     
     

    I have a VSTO project for Excel 2007 binded to .NET 3.5 and now we're going to upgrade the framework to 4.0 leaving version of Office the same, i.e. 2007.

    If it's possible how I can do it?

All Replies

  • Thursday, September 02, 2010 10:03 PM
    Answerer
     
     

    Yes, this is certainly possible.

    • If you created the project in Visual Studio 2010, then you simply need to retarget the project to the .NET Framework 4. For instructions, see http://msdn.microsoft.com/en-us/library/bb398202.aspx.
    • If you created the project in an earlier version of Visual Studio, you'll need to upgrade the project to Visual Studio 2010. When you open the project in Visual Studio 2010, the target framework will automatically change to the .NET Framework 4 if the .NET Framework 3.5 is not installed on the development computer. If the .NET Framework 3.5 is installed, the upgraded project will still target the .NET Framework 3.5, but you can go in and change the target framework yourself by following the same instructions above. For more details, see http://msdn.microsoft.com/en-us/library/k2xkefex.aspx.

    For either case, you'll most likely also have to make some manual changes to your code after you change the target framework to the .NET Framework 4. For more details, see http://msdn.microsoft.com/en-us/library/ee207231.aspx.

    Another consideration is which version of Office you have installed on the development computer. If you have Office 2007 installed on the develoment computer, then an Office 2007 project you created in an earlier version of Visual Studio will remain an Office 2007 project when you open it in Visual Studio 2010. However, if you have Office 2010 installed, then by default Visual Studio will modify the project to target Office 2010 (i.e. it will reference the Office 2010 PIAs, the project debug settings will point to Excel 2010 on your development computer, etc.). In this scenario, if you want the project to keep targeting Office 2007 you need to change the Always upgrade to installed version of Office property before you open the project in Visual Studio 2010.


    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Thursday, September 02, 2010 11:25 PM
     
     

    In my case it's not enough. I have the following VS2008-generated code in my VSTO:

    [Microsoft.VisualStudio.Tools.Applications.Runtime.

     

    StartupObjectAttribute(0)]

    [

     

    global::System.Security.Permissions.PermissionSetAttribute(global::System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]

     

     

    public sealed partial class ThisWorkbook : Microsoft.Office.Tools.Excel.Workbook, Microsoft.VisualStudio.Tools.Office.IOfficeEntryPoint {

    And VS2010 complains every time about this code when I try to target the VSTO at .NET 4.0:

    ThisWorkbook.Designer.cs(21,116): error CS0234: The type or namespace name 'Office' does not exist in the namespace 'Microsoft.VisualStudio.Tools' (are you missing an assembly reference?)

    How to resolve this issue?

     

  • Friday, September 03, 2010 2:51 AM
     
     

    In my case it's not enough. I have the following VS2008-generated code in my VSTO:

    [Microsoft.VisualStudio.Tools.Applications.Runtime.

     

    StartupObjectAttribute(0)]

    [

     

     

    global::System.Security.Permissions.PermissionSetAttribute(global::System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]

     

     

     

    public sealed partial class ThisWorkbook : Microsoft.Office.Tools.Excel.Workbook, Microsoft.VisualStudio.Tools.Office.IOfficeEntryPoint {

    And VS2010 complains every time about this code when I try to target the VSTO at .NET 4.0:

    ThisWorkbook.Designer.cs(21,116): error CS0234: The type or namespace name 'Office' does not exist in the namespace 'Microsoft.VisualStudio.Tools' (are you missing an assembly reference?)

    How to resolve this issue?

     

    Hi,

    Has the project a reference to Microsoft.VisualStudio.Tools.Applications.Runtime.dll?

    Regards,

    Mauro.

  • Friday, September 03, 2010 3:31 AM
     
     
    No, it's not available even if to open "Add Reference" dialogbox. Where can I get it?
  • Friday, September 03, 2010 3:37 AM
     
     

    Look for C:\Program Files\Reference Assemblies\Microsoft\VSTO40\v4.0.Framework\Microsoft.VisualStudio.Tools.Applications.Runtime.dll.

    Let me know if this worked for you. 

  • Friday, September 03, 2010 4:04 AM
     
     
    Nope, doesn't help. Microsoft.VisualStudio.Tools.Applications.Runtime.dll seems not to have

    Microsoft.VisualStudio.Tools.Office.IOfficeEntryPoint

  • Friday, September 03, 2010 4:13 AM
     
     
    Nope, doesn't help. Microsoft.VisualStudio.Tools.Applications.Runtime.dll seems not to have

    Microsoft.VisualStudio.Tools.Office.IOfficeEntryPoint

    Ok, try removing that interface from the inheritance and updating to WorkbookBase, as said in the article you already commented about this issue too.

    public sealed partial class ThisWorkbook : Microsoft.Office.Tools.Excel.WorkbookBase {

     

     

  • Friday, September 03, 2010 4:10 PM
    Answerer
     
     Answered Has Code

    Something is certainly amiss here. When you have an Excel Workbook project that targets the .NET Framework 3.5 and then you retarget it to the .NET Framework 4 (by following the instructions in http://msdn.microsoft.com/en-us/library/bb398202.aspx), Visual Studio should automatically update the generated code in the ThisWorkbook.Designer.cs file (and the other "Designer" code-behind files) so that your ThisWorkbook and Sheetn classes derive from teh appropriate base classes in the .NET 4 extensions in the VSTO runtime. After you regarget your project, the declaration for ThisWorkbook in the ThisWorkbook.Designer.cs file should be automatically changed from what you showed earlier in this thread to the following code.

    [Microsoft.VisualStudio.Tools.Applications.Runtime.StartupObjectAttribute(0)]
    [global::System.Security.Permissions.PermissionSetAttribute(global::System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
    public sealed partial class ThisWorkbook : Microsoft.Office.Tools.Excel.WorkbookBase {
    

    The only manual code changes you should need to make yourself are the ones described under this section of the docs: http://msdn.microsoft.com/en-us/library/ee207231.aspx. To clarify the comment in the previous post in this thread, the information about WorkbookBase (and other xxxBase classes) in http://msdn.microsoft.com/en-us/library/ee712587.aspx is specifically about updating any code you wrote that passes around instances of the ThisWorkbook/Sheetn classes in your project. You should not need to modify the declaration of the ThisWorkbook/Sheetn classes in the code-behind files yourself.

    As for why Visual Studio is not changing this code for you, that's what we need to figure out. Some things to consider that might help:

    • Does this problem only occur with this specific project, or does it also occur if you create a brand new Excel Workbook project that targets .NET 3.5 and then retarget it to .NET 4?
    • How are you changing the target framework? Are you following the instructions in the topic I linked to above, or are you doing something else?
    • Are you using the RTM version of Visual Studio 2010 (build# 10.0.30319), or are you by chance still using an RC or Beta build? There were a number of retargeting bugs in builds prior to RTM.
    • If you are using the RTM version, did you at any time have an RC or Beta version installed on your development computer before you installed the RTM version?

     


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Proposed As Answer by Amy LiModerator Tuesday, September 07, 2010 6:27 AM
    • Marked As Answer by Senglory Tuesday, September 07, 2010 8:38 PM
    •  
  • Tuesday, September 07, 2010 6:27 AM
    Moderator
     
     
    Hi Senglory,

    How is this issue going on in your side? Have you solved the problem?

    Best Regards,
    Amy Li
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Tuesday, September 07, 2010 4:41 PM
     
      Has Code

    HI!

     

    I'm still in the middle of that kind of manual migration. The current issue I've faced with is the following code in my Ribbonbar:

     

    this.buttonScenarioQkConfigure.Click += new System.EventHandler<Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs>(this.buttonScenarioQkConfigure_Click);
    

    And this leads to the following error message:

    error CS0311: The type 'Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs' cannot be used as type parameter 'TEventArgs' in the generic type or method 'System.EventHandler'. There is no implicit reference conversion from 'Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs' to 'System.EventArgs'.

  • Tuesday, September 07, 2010 5:25 PM
     
     Answered Has Code
    Hi,
    
    Update the event handler as this:
    
    this.buttonScenarioQkConfigure.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(this.buttonScenarioQkConfigure_Click);
    
    ...
    
    buttonScenarioQkConfigure_Click (object sender, RibbonControlEventArgs e)
    {
    }

    Regards,

    Mauro.
    • Marked As Answer by Senglory Tuesday, September 07, 2010 8:38 PM
    •  
  • Tuesday, September 07, 2010 6:23 PM
     
     

    Thank you , guys! The migration was done after I compiled info from the following 2 articles:

     

    http://msdn.microsoft.com/en-us/library/ee712587.aspx

     

    and

    http://msdn.microsoft.com/en-us/library/ee712589.aspx
  • Tuesday, September 07, 2010 6:29 PM
     
     
    You're welcome. If any of the reponses actually helped you to resolve your issue, please mark it (or them) as answer(s).
  • Tuesday, September 07, 2010 10:07 PM
    Answerer
     
     

    Senglory - I'd like to follow up on the original error you reported in this thread, where the declaration of the ThisWorkbook class in your code-behind file was not getting automatically updated by Visual Studio when you changed the target framework. Did Visual Studio wind up changing this code for you, or did you fix this by changing the code manually?

    If Visual Studio did not change this code for you, can you please provide answers to the following questions?

    • Does this problem only occur with this specific project, or does it also occur if you create a brand new Excel Workbook project that targets .NET 3.5 and then retarget it to .NET 4?
    • How are you changing the target framework? Are you following the instructions in the topic I linked to above, or are you doing something else?
    • Are you using the RTM version of Visual Studio 2010 (build# 10.0.30319), or are you by chance still using an RC or Beta build? There were a number of retargeting bugs in builds prior to RTM.
    • If you are using the RTM version, did you at any time have an RC or Beta version installed on your development computer before you installed the RTM version?

    Thanks - I'm basically trying to determine whether there might be a bug we need to follow up on internally. As I said before, Visual Studio should change the ThisWorkbook definition for you when you retarget your project; you should never have encountered this particular compile error.


    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Tuesday, September 07, 2010 10:55 PM
     
     

    Hi McLean,

    Below are my answers:

    - Does this problem only occur with this specific project, or does it also occur if you create a brand new Excel Workbook project that targets .NET 3.5 and then retarget it to .NET 4?

    It happens with every VSTO Excel 2007 project created in VS 2008


    - How are you changing the target framework? Are you following the instructions in the topic I linked to above, or are you doing something else?

    Exactly to the link you pointed.


    Are you using the RTM version of Visual Studio 2010 (build# 10.0.30319), or are you by chance still using an RC or Beta build? There were a number of retargeting bugs in builds prior to RTM.
    If you are using the RTM version, did you at any time have an RC or Beta version installed on your development computer before you installed the RTM version?

     

    I'm working with pure RC build (10.0.30319) on a machine where no pre-RC VS 2010 has been ever installed.

     

  • Friday, September 10, 2010 7:35 PM
    Answerer
     
     

    I tested this out by taking an Excel 2007 Workbook project that I originally created in Visual Studio 2008, and opening this project on a computer with Visual Studio 2010 installed. When I opened the project, Visual Studio automatically upgraded it to a Visual Studio 2010 project. After I retargeted the project to the .NET Framework 4, Visual Studio automaticlaly updated the code-behind files as expected (i.e., ThisWorkbook now derives from WorkbookBase). So, this scenario appears to work as expected on my end.

    If you create a brand new Excel 2007 Workbook project that targets the .NET Framework 3.5 in Visual Studio 2010, and then retarget this project to the .NET Framework 4, do you get the same issue? Or are you only seeing this with projects that you originally created in Visual Studio 2008?

    Do you have both Visual Studio 2008 and Visual Studio 2010 installed on the development computer where you are running into this issue, or just Visual Studio 2010?

     


    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Friday, September 10, 2010 8:24 PM
     
     
    Yes, this is exactly my case. I have VS 2008 ,Office 2007 and VS 2010 installed on the same development machine
  • Friday, September 10, 2010 9:40 PM
    Answerer
     
     
    If you create a brand new Excel 2007 Workbook project that targets the .NET Framework 3.5 in Visual Studio 2010, and then retarget this project to the .NET Framework 4, do you get the same issue? Or are you only seeing this with projects that you originally created in Visual Studio 2008? Thanks.
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Monday, September 13, 2010 5:33 PM
     
     
    No, the issue's gone. But I seem to have found the reason for this behaviour. My files in 2008 project were VSS-bound and I didn't get them on checkout presuming that the'd be checked out in VS 2010 automatically. 
  • Monday, September 13, 2010 5:40 PM
    Answerer
     
     
    That does sound like it could have caused the issue. Thanks for replying back with the information.
    This posting is provided "AS IS" with no warranties, and confers no rights.