|
-
May 17th, 2013, 03:01 PM
#1
Thread Starter
Addicted Member
[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?)
-
May 17th, 2013, 05:28 PM
#2
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 :]
-
May 17th, 2013, 06:34 PM
#3
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!
-
May 26th, 2013, 04:13 PM
#4
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|