Results 1 to 4 of 4

Thread: [RESOLVED] Detecting Excel version

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    152

    Resolved [RESOLVED] Detecting Excel version

    My application allows data to be exported to Excel. I wrote it on a computer with Excel 2007 so it exported files as *.xlsx by default; now I'm opening it on a computer with Excel 2003 and of course that doesn't work and I need to export as *.xls.

    My question is: Can I detect the Excel version so that I can automatically have the export form default to the correct filetype?

    (Side question: When I originally wrote it on the other computer using Visual Basic.NET, I needed to import Microsoft.Office.Interop. Now, working on it from a different computer using Visual Studio 2012, I don't need to anymore and it yields a warning. Why is that?)

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,375

    Re: Detecting Excel version

    Why not take a different approach, when you say export, I'm assuming you mean that you're using a SaveFileDialog. If so, then set your filter to:
    Code:
    .Filter = "Excel 2003(*.xls)|Excel 2007(*.xlsx)|*.xlsx"
    Then use a try/catch to see if an exception gets thrown. If it does throw an exception, then try it again in 2003:
    Code:
    Dim sfd As New SaveFileDialog
    sfd.Filter = "Excel 2003(*.xls)|Excel 2007(*.xlsx)|*.xlsx"
    
    If sfd.ShowDialog = OK Then
       If sfd.FilterIndex = 1 Then '2007
          Try
             'Try running '07 code
          Catch ex As Exception
             'Try running '03 code
             Try
    
             Catch new_ex As Exception
                MessageBox.Show(String.Format("2003:{0}{1}{0}2007:{2}", Environment.NewLine(), new_ex, ex))
             End Try
          End Try
    End If
    I'm sure there's a better way of doing it, but there's one way :]
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Detecting Excel version

    yields a warning. Why is that?
    The exact wording of the warning would help in determining this.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    152

    Re: [RESOLVED] Detecting Excel version

    Figured this out; in case anyone's wondering, here's how:

    Code:
            Dim saveFileDialog1 As New SaveFileDialog()
            'checks the Excel version
            Dim XLApplication As Excel.Application
            XLApplication = DirectCast(CreateObject("Excel.Application"), Excel.Application)
            If CInt(XLApplication.Version) >= 12 Then '2007: defaults to .xlsx
                saveFileDialog1.Filter = "Excel files (*.xlsx)|*.xlsx|Excel 2003 files (*.xls)|*.xls|All files (*.*)|*.*"
            Else 'earlier version: defaults to .xls
                saveFileDialog1.Filter = "Excel 2003 files (*.xls)|*.xls|All files (*.*)|*.*"
            End If

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