Results 1 to 5 of 5

Thread: [RESOLVED] [1.0/1.1] Late Bind - Convert VB.NET to C#

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Resolved [RESOLVED] [1.0/1.1] Late Bind - Convert VB.NET to C#

    I am late binding Excel in my app and I need to fix a few errors. I have the app instance created fine but I need to access other properties and methods of it and I get errors saying "some method is not defined for object".

    Thanks


    VB Code:
    1. object oWB = moApp.[u]Workbooks[/u].Add();
    2. oWB.[u]Sheets[/u]("Sheet1").Cells(1, 1).Value = "Meow!";
    Last edited by RobDog888; Aug 11th, 2006 at 09:23 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [1.0/1.1] Late Bind - Convert VB.NET to C#

    I still havent got the first posts code but I am making progress on these parts but still having trouble converting.


    VB Code:
    1. private object moApp;
    2. private System.Type moAppType;
    3.  
    4. /// Create the late bound app object
    5. moAppType = System.Type.GetTypeFromProgID("Excel.Application");
    6. object moApp = System.Activator.CreateInstance(moAppType);
    7.  
    8. /// ...
    9.  
    10. /// Closing of form but this line wont work for late binding
    11. if ((moApp.Workbooks.Count) > 0)
    12.  
    13. /// So I converted it to this but not working
    14. if (moAppType.InvokeMember("Workbooks.Count", System.Reflection.BindingFlags.GetProperty, null, moApp, null) > 0)
    15. [Error: Operator '>' cannot be applied to operands of type 'object' and 'int']
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [1.0/1.1] Late Bind - Convert VB.NET to C#

    Woot! I got another error fixed.

    The moApp.Workbooks.Add() method converts to late binding in this way.


    VB Code:
    1. [color=dimgray]///[/color] [color=darkgreen]Original C# code:[/color]
    2. moApp.Workbooks.[color=black]Add[/color]();
    3.  
    4. [color=dimgray]///[/color] [color=darkgreen]Converted to late binding:[/color]
    5. moAppType.InvokeMember("Workbooks.Add", System.Reflection.BindingFlags.[b]InvokeMethod[/b], [color=blue]null[/color], moApp, [color=blue]null[/color]);
    I cant test it yet as I have a couple of more errors but it compiles.

    I guess you can call child members without drilling down the object heirachy?

    MS on System.Type.InvokeMember
    Last edited by RobDog888; Aug 12th, 2006 at 01:09 AM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: [1.0/1.1] Late Bind - Convert VB.NET to C#

    This should work:
    if (System.Convert.ToInt32(moAppType.InvokeMember("Workbooks.Count", System.Reflection.BindingFlags.GetProperty, null, moApp, null)) > 0)
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  5. #5

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] [1.0/1.1] Late Bind - Convert VB.NET to C#

    I found that I can not reference the property without transversing the object heirarchy first. I got around getting the count property this way.

    VB Code:
    1. if (System.Convert.ToInt32(oWBs.GetType().InvokeMember("Count", BindingFlags.InvokeMethod | BindingFlags.GetProperty, null, oWBs, null)) > 0)

    Still was hung up on the casting though as I am still learning C#.

    Thanks David
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width