Creating Excel.XlFileFormat as a proper Object MS Office Automation

Below are two code snippets. The first part works fine when including the Excel library in my VS project.
The problem is I have multiple versions of MS Office in use so I have to use the second method by GetObject or CreateObject to use whatever version the user has installed.
I'm wondering how i can properly create the object for the XlFileFormat.
The second code snippet fails when creating the object. Not sure how to work around this as the error states cannot create ActiveX control. Thanks in advance.


Snippet A - Works
 
Dim objExcelApp As New Excel.Application
        Dim objWorksheet As New Excel.Worksheet
        Dim objXFileFormat As New Excel.XlFileFormat
 
        With objExcelApp
            .Visible = True
            .Workbooks.Add(PATH)
            objWorksheet = .Worksheets(.ActiveSheet.Name)
            objWorksheet.SaveAs("c:\testxls", objXFileFormat.xlExcel9795)
        End With
 
Snippet B - Not Working
 
     Dim objExcelApp As Object
        Dim objWorksheet As Object
        Dim objXFileFormat As Object
 
        Try
            objExcelApp = GetObject("Excel.Application")
            objXFileFormat = GetObject("Excel.XlFileFormat")
        Catch e As Exception
            objExcelApp = CreateObject("Excel.Application")
''Fails Here with a cannot create ActiveX Error
            objXFileFormat = CreateObject("Excel.XlFileFormat")
        End Try
 
       With objExcelApp
            .Visible = True
            .Workbooks.Add(PATH)
            objWorksheet = .Worksheets(.ActiveSheet.Name)
            objWorksheet.SaveAs("c:\testxls", objXFileFormat.xlExcel9795)
        End With

Open in new window

cbish21577Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulHewsCommented:
You can look up constant definition in the online help, or using a source like: http://nerds-central.blogspot.com/2007/02/excel-constant-definitions-for-vbscript.html


Const xlExcel9795 As Integer = &H2B      
objWorksheet.SaveAs("c:\test.xls", xlExcel9795)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cbish21577Author Commented:
Thanks Paul, Worked Perfectly!
    Dim objExcelApp As Object
            objExcelApp = CreateObject("Excel.Application")
           Const xlExcel9795 As Integer = &H2B

        With objExcelApp
            .Visible = True
            .Workbooks.Add(PATH)
            objWorksheet = .Worksheets(.ActiveSheet.Name)
            objWorksheet.SaveAs("c:\testxls", xlExcel9795)
        End With
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.