Results 1 to 9 of 9

Thread: [RESOLVED] Opening Specific Version of Excel

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2004
    Posts
    9

    Resolved [RESOLVED] Opening Specific Version of Excel

    I'm opening an instance of Excel to import data from a workbook using the following approach in VBA:

    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlWSheet As Excel.Worksheet

    Set xlApp = New Excel.Application
    Set xlBook = xlApp.Workbooks.Open(FileName, False)
    Set xlWSheet = xlBook.Worksheets(1)

    The problem is that the workbook is an Excel 2003 document, but the machine running the code has both 2003 and 2007 versions of Excel. The Excel instance opens the document in 2007 causing errors, which crash the code.

    Is there a simple way of specifying in code which version of Excel to create an instance of?

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Opening Specific Version of Excel

    The problem is that the workbook is an Excel 2003 document, but the machine running the code has both 2003 and 2007 versions of Excel.
    Both Cannot exist together without giving errors... you will have to uninstall one of them...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2004
    Posts
    9

    Re: Opening Specific Version of Excel

    Actually they exist together fine as long as I open the Excel version that I want first and then choose the document that I want to work with. Unisntallation is unfortunately not an option.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Opening Specific Version of Excel

    you could shell with full path and filename, to open the correct version of excel then use getobject(filename) to to work with that instance of excel

    alternatively you could create a shortcut to open that workbook in the correct version of excel
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Opening Specific Version of Excel

    Actually I think you can do it fairly easily, just change this line:
    Code:
    Set xlApp = New Excel.Application
    to this:
    Code:
    Set xlApp = CreateObject("Excel.Application.11")
    The number at the end can be one of the following:
    7 - Excel 95
    8 - Excel 97
    9 - Excel 2000
    10 - Excel XP
    11 - Excel 2003
    ..and presumably 12 for Excel 2007, but I haven't checked yet.

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2004
    Posts
    9

    Re: Opening Specific Version of Excel

    Thanks for the suggestions guys. The suggestion by si_the_geek should have worked, but it didn't. Makes me think the end users are doing something strange (works fine on my machine since I don't have 2007 on mine).

    I'll let you all know how it turns out.

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

    Re: Opening Specific Version of Excel

    Yes, 12 is for 2007.

    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

  8. #8
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Opening Specific Version of Excel

    Install multiple versions of Excel (and Access too) on the same machine will give terrible headaches for novice users.

    Specify version xx in CreateObject("Excel.Application.xx") won't help.

    The culprit is even with 2 versions of Excel installed (2003=11 & 2007=12), the registry contains only one ProgID entry for Excel.

    To switch it manually, run one of these shortcuts:
    "C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE" /regserver
    or
    "C:\Program Files\Microsoft Office\OFFICE12\EXCEL.EXE" /regserver

    To switch it programmatically, you need to have a piece of code to change the value of this registry entry:

    HKEY_CLASSES_ROOT\CLSID\{00024500-0000-0000-C000-000000000046}\ProgID

    to either "Excel.Application.11" or "Excel.Application.12"

    before using CreateObject("Excel.Application").
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  9. #9

    Thread Starter
    New Member
    Join Date
    Dec 2004
    Posts
    9

    Re: Opening Specific Version of Excel

    Thanks for all the great suggestions. It works fine now. The problem came down to source files being sent to us as Excel 2.1 (don't ask me) and VBA was having problems opening them in Excel 2003. Having Excel 2007 on the same machine just muddied the waters even more.

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